Python Forum
Impossible Function Task?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Impossible Function Task?
#1
I have recently taught myself to use functions, and tried to do the impossible, this task:

Shhh. They are listening! We need a plan…
I know, let us use a cipher to hide our conversations. That way they will not understand what we say.
The cipher works like this. You take a sentence and split it into two lists. One has the even words, the other the odd words. All punctuation is ignored.
So for example

"Hello world! I am pleased to meet you!"
odd = [“Hello”, “I” ,”pleased” ,”meet”]
even = [“world”, “am”, “to”, “you”]
You then combine the two lists using these rules –
  • Take the last word of each list.
    Take the last letter of each word and place them next to each other. One from each word (starting with odd)
    Repeat until one word is out of letters.
    Place a “!” symbol if it belonged to the odd word or a “#” if it belonged to the even and then copy the rest of the letters.
    Place a space in the output
    Repeat until one list is empty
    Finally copy the last word (if any) over to the output.
So the result of these rules would be –

"Hello world! I am pleased to meet you!"
odd = [“Hello”, “I” ,”pleased” ,”meet”]
even = [“world”, “am”, “to”, “you”]
output = “tueoey!m doet!saelp im#a odlllreohw”
You are to write two functions. One which will encrypt a sentence and one which will decrypt one. This is a hard problem! Make sure you can decipher the text before you begin the code.

So far, I have done this much:
def encrypt(inp):
    for x in string.punctuation:
        inp=inp.replace(x,"")
    inp=inp.split()
    odd=[]
    even=[]
    for x in range(0,len(inp)):
        if x % 2 == 0:
            odd.append(inp[x])
        elif x % 2 != 0:
            even.append(inp[x])
    
    return odd,even

import string

#sentence=input("What is your sentence?")
sentence="Hello world! I am pleased to meet you!" 
print(encrypt(sentence))
Any tips on how I should go about doing the next step, thanks!

Feel free to try yourselves.
Reply
#2
(Jan-02-2018, 10:36 PM)ShadowWarrior17 Wrote: The cipher works like this. You take a sentence and split it into two lists. One has the even words, the other the odd words. All punctuation is ignored.
So for example

"Hello world! I am pleased to meet you!"
odd = [“Hello”, “I” ,”pleased” ,”meet”]
even = [“world”, “am”, “to”, “you”]


What determines a word's oddity/evenness? Why is "meet" odd, and "world" even? "meet" has 4 characters, so I'd assume it's even...
Reply
#3
Oddity / evenness is defined by the position of the word in the sentence...

I would write a function that takes two words as input and returns the crypted output. You could write this function with an index for the letter position, or if you like the challenge, you could write a recursive function.
Reply
#4
There is a missing instruction: the last word (in case of an odd number of words) must be encrypted with the prefix symbol "#" to properly decipher the message. The reason is that when you see the last encrypted word, you cannot tell if it is the result of the fusion of the last two words or just the last odd word in reverse order.
Reply
#5
(Jan-03-2018, 09:23 AM)squenson Wrote: when you see the last encrypted word, you cannot tell if it is the result of the fusion of the last two words or just the last odd word in reverse order.

As far as I understand it - in case of odd number of words, the encrypted massage will also have odd number of words, the last word has no # or ! and it is not changed at all - just copied in reverse. So it is not problem to decipher, no missing instructions.
However, in my opinion given sample output should be
output = “tueoey!m doet!saelp im#a odlllreoh!w”

note the ! in odlllreoh!w
Reply
#6
Quote:in case of odd number of words, the encrypted massage will also have odd number of words
I am afraid that this is not correct, as the number of words in the encrypted message is equal to the number of words in the initial message divided by two, plus one if there is an odd number of words. So: 1 for 1, 1 for 2, 2 for 3, 2 for 4, 3 for 5, 3 for 6, etc.
Reply
#7
(Jan-03-2018, 12:04 PM)squenson Wrote: I am afraid that this is not correct
you are right, my mistake
Reply
#8
yhrceuvm dtei!kil di!na gensiitcsreerxee!tni ay#rev ssiahw!t
Reply
#9
So without modifying the rules, the task in itself is impossible?
Reply
#10
No, don't worry it is just a clarification to cover the case of an odd number of words.

The sentence above encrypted:
fsod#row drdeob#mun fnoa eehsta#c orte#voc an#oitacifiralc stis#uj ytri!row otnn#od
and decrypted:
no dont worry it is just a clarification to cover the case of an odd number of words
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mission Impossible : New to Python and 3 days to do theses exercises Kangaaxx 6 3,703 Aug-16-2018, 05:58 PM
Last Post: Kangaaxx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020