-5

OK, so I have to make a program in python, that is able to make a number for each word in a sentence, and if the word is the same it will have the same number, but I have to be able to change it back to the original sentence as well.

For example, "today the cat sat on the mat" would be changed into 0 1 2 3 4 1 5

0 being "today" and 5 being "mat", and 1 being "the"

so basically if I get the idea of this its creating a variable for each word being a number, the only problem is I have no idea were to start making this program,

some help would really be appreciated thanks :)

Dummy8123
  • 17
  • 2
  • 6
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a coding or tutorial service. – Prune Oct 11 '16 at 16:52
  • 1
    Just out of pure curiosity, would another example be `'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'`? – jonrsharpe Oct 11 '16 at 16:55
  • It would be great to see some of your attempts. Have you attempted to solve the problem? – devtye Oct 11 '16 at 16:57
  • ok lets kill 3 bird in one stone here number 1st I have seen many question on the website which have been a sort of "code service", none of which have any sort of comment saying its not part of the rules, 2nd yes ASK NOT......... is a very good example of this, and 3rd yes I have tried many attempts none of which have succeeded. – Dummy8123 Oct 16 '16 at 20:17

1 Answers1

1

This sounds a lot like a school assignment. Since from my experience I can say that the best practice is to do it yourself, I'd suggest only looking at the hints I give first, and if you're really stuck, look at the code.

Hint 1:

Separate the sentence into a list of words first.

You can do this using

words = sentence.split(" ")

Hint 2:

You want to create some kind of mapping from word to number, but also in reverse.

You can do this using

dicts. Treat them as literal dictionaries: Make one dict with the words as keys and the numbers as values, and one with numbers as keys and words as values. That'll allow you to look up numbers and words as necessary. Note that the dict which has numbers as keys could theoretically be a list, however this might break when you don't want numbers anymore, or when you want to delete certain entries.

Hint 3:

You'll need to generate an entry in both dicts mentioned in Hint 2 for every word - to make sure you can go back. Thus, a for loop over the list with words, and at every iteration generate an entry in both dicts.

Hint 4:

In order to make sure same words map to the same number, there are two ways to go. Firstly, during the iteration, you can simply check if the word is already in the words->numbers dict. If it is, skip it. Otherwise, generate the entries. Note that you need to keep track of the highest number you have used. Secondly, you could convert the list with words to a set. This will directly remove all duplicates, though you may lose the ordering of the words, meaning that in your example it might become 3 2 0 5 4 2 1

I hope this will be useful. If absolutely necessary, I could provide the code to do it, but I highly recommend figuring it out yourself.

Lolgast
  • 329
  • 2
  • 10
  • s = "hey there my name is bob" s = [val + " = " + str(index) for index, val in enumerate(s.split())] print(s) – Dummy8123 Oct 13 '16 at 19:13
  • s = "you ask my name then ill ask yours" s = [val + " = " + str(index) for index, val in enumerate(s.split())] print(s) – Dummy8123 Oct 13 '16 at 19:14
  • sorry cant delete the other comments my computer was playing up my code so far is s = "hey there my name is bob" s = [str(index+1) for index, val in enumerate(s.split())] print(s) however this doesn't seem to be working the way I wanted it to work :(, please help me it doesn't account nfor the repeated word, and I also don't understand what you meant about mapping a word to a number, I'm desperate now thanks – Dummy8123 Oct 13 '16 at 19:19
  • What I mean with mapping is that you generate a 'dictionary', such that for every word, you can look up which number is assigned to it, and for every number, which word is assigned to it. The reason why your code doesn't account for the repeated word is simply that you don't instruct your program to do so. See hint 4 for some information on how to do so. As a side note, I wouldn't suggest using list comprehensions (the `[f(var) for var in list]` part) for anything other than simple operations, because you quickly lose overview. – Lolgast Oct 15 '16 at 09:31