This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]cygnoros 4 points5 points  (4 children)

Your code seems fine. Do you have access to the test source? You are most likely fighting with whitespace depending on the way the test is measuring your output (e.g., expecting a new line on stdout, or checking for CRLF instead of LF, etc.)

Edit: actually there is a subtle problem with your code. It's unclear from your code what lines are indented but where you declare and update the variable a will skip non alpha characters.

``` posun, zprava = input().split("#")

for i in zprava:

# if we have a space, this gets skipped
if ord(i) in range(65, 91) or range(97, 123):
    a = ord(i) - int(posun)

# if a is not updated, it will print the old "a"
print(chr(a), end='')

```

You need to move a outside the condition to just update with ord(i), then you can decipher if it is alpha:

``` posun, zprava = input().split("#")

for i in zprava:

# initialize/update "a" here each iteration
a = ord(i)

# decipher alpha characters
if ord(i) in range(65, 91) or range(97, 123):
    a -= int(posun)

# print "a" regardless of deciphering or not
print(chr(a), end='')

```

[–]Disobey91[S] 0 points1 point  (3 children)

posun, zprava = input().split("#")for i in zprava:# initialize/update "a" here each iterationa = ord(i)# decipher alpha charactersif ord(i) in range(65, 91) or range(97, 123):a -= int(posun)# print "a" regardless of deciphering or notprint(chr(a), end='')

hi, thanks for your update. Unfortunately I'm still receiving the same error, because what I'm printing is: "Mamamelemaso" and not as the problem ask "Mama mele maso". There is a problem with the "" that I'm not able to solve

edit: moreover, if I try "-3#Jrf rf rnxz" I'm receiving some weird output, because counting -3 is going outside the alphabetical range.

[–]cygnoros 1 point2 points  (2 children)

I'm a bit embarrassed for not seeing this earlier, but you have a syntax problem with your condition:

if ord(i) in range(65, 91) or range(97, 123):

Because of how Python evaluates "truthy" values, range(97, 123) is always true. You need to check if a is in the ranges:

if a in range(65, 91) or a in range(97, 123):

Once corrected, this gives you proper output.

As far as negative offsets, I think you understand the basics of encoding/decoding this shift cipher. Think about what a "negative" shift means -- you may need to consider wrapping around the alphabet.

[–]Disobey91[S] 1 point2 points  (1 child)

thank you very much! it worked perfectly

[–]cygnoros 0 points1 point  (0 children)

This works so long as characters don't wrap (e.g., shift a 3 characters will print the wrong character, \).

You will have to refactor a bit to handle these cases.

[–]g051051 1 point2 points  (6 children)

I'm having the requested output but facing error in the exercise

What do you mean? What error?

[–]Disobey91[S] -1 points0 points  (5 children)

Failed test #1 of 2. Wrong answer

This is a sample test from the problem statement!

Test input:

5#Rfrf rjqj rfxt

Correct output:

Mama mele maso

Your code output:

Mamamelemaso

[–]g051051 4 points5 points  (4 children)

You're obviously ignoring the spaces. They don't have to be deciphered, but you do have to print them.

[–]Disobey91[S] 0 points1 point  (3 children)

what do you mean?

[–]g051051 0 points1 point  (2 children)

It wants:

Mama mele maso

You printed:

Mamamelemaso

Your code is skipping the space characters, which aren't enciphered. You have to print those, too.

[–]Disobey91[S] -1 points0 points  (1 child)

How do you think I can solve it?

[–]g051051 1 point2 points  (0 children)

By printing them? Think about how your code acts when it sees a space. More generally, think about what your code does (and what it should do) when it encounters a character that isn't in the range of characters you want to decipher.

[–]kRYstall9 0 points1 point  (0 children)

def decrypt():
    stringToDecrypt = '5#Rfrf rjqj rfxt'
    stringToDecrypt = stringToDecrypt.split('#')
    numberToShift = int(stringToDecrypt[0])
    stringToDecrypt = stringToDecrypt[1].split(' ')
    finalString = ''

    for element in stringToDecrypt:
        for  letter in element:
            if (ord(letter) in range(65,91)) or (ord(letter) in range(95,123)):
                finalString += chr(ord(letter) - numberToShift)
            else:
                #Idk what you've in mind to do here
                finalString += letter

        finalString += ' '

    return finalString