Help needed for a first-year computer science exercise

SalaNsi Posted messages 13 Status Member -  
SalaNsi Posted messages 13 Status Member -

Hello,

I am in my first year of high school and I have a homework assignment that contains this exercise; I couldn't figure it out even after trying for a long time.

I have linked what I have completed along with the instructions.

Thank you!

7 answers

  1. jee pee Posted messages 31961 Registration date   Status Moderator Last intervention   10 008
     

    Hello,

    Rather than an image of the code, we prefer it to be in the message, with the source icon and the Python language which gives:

    for i in range(4): print(i)

    Your statement indicates that the power of 2 is a positional calculation from right to left, so you cannot simply take i. After that, you need to extract the position of the binary with a binary[i] syntax.

    0
  2. SalaNsi Posted messages 13 Status Member
     

    I understand the code in the message, but after that I don't see how to do it from right to left, and even less how to extract the position of the binary since we haven't covered it in the course.

    0
    1. jee pee Posted messages 31961 Registration date   Status Moderator Last intervention   10 008
       

      Depending on the value of i, from 0 to 3, you will extract the bit to be processed with binaire[i]

      Next, for the power of 2, you need to find a formula that combines i (varying from 0 to 3) and len(binaire) which equals 4 where

      • when i=0 power of 2 = 3
      • when i=1 power of 2 = 2
      • when i=2 power of 2 = 1
      • when i=3 power of 2 = 0
      0
  3. SalaNsi Posted messages 13 Status Member
     

    I'm sorry, but even after searching for a long time, I still haven't found what needs to be put.

    0
    1. jee pee Posted messages 31961 Registration date   Status Moderator Last intervention   10 008
       
      int(binary[i]) * (2**(len(binary)-1-i))

      You need to take the Python course again.

      0
  4. SalaNsi Posted messages 13 Status Member
     

    Thank you for that, but honestly I'm not lying to you, I have 3 other assignments in the homework and I don't know what the teacher has in mind, we don't have the course, he just gave us some tools to supposedly help us.

    0
  5. SalaNsi Posted messages 13 Status Member
     

    I even started the 2nd exercise but I am still having difficulties

    here is the code for the 2nd and the instructions linked

    :

    def dec_to_bin(decimal):
    binary=""
    quotient = decimal // 2
    remainder = decimal % 2
    binary = remainder + binary
    while quotient!=0 :
    decimal = quotient
    remainder = decimal % 2
    binary = remainder + binary
    quotient = decimal//2


    # in the end, as long as the binary does not have a size of 4
    while len(binary) != 4:
    # we add "0"s to the left
    binary = ...

    return binary


    # The lines below test your code... don't touch them!
    # If an AssertionError appears... it means your code is not correct...
    assert dec_to_bin(10) == '1010'
    assert dec_to_bin(15) == '1111'
    assert dec_to_bin(0) == '0000'

    0
  6. PierrotLeFou
     

    I didn't check if this site offers a Python course.
    Search for the site "Zeste De Savoir" the course "Zeste de Python" written by "entwanne".

    0
  7. SalaNsi Posted messages 13 Status Member
     

    but actually I don't have much time because the assignment is due on Monday.

    0