-1

My task is to:

Develop a program that identifies individual words in a sentence, stores these in a list and replaces each word in the original sentence with the position of that word in the list.

For example, the sentence ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY contains the words ASK, NOT, WHAT, YOUR, COUNTRY, CAN, DO, FOR, YOU

The sentence can be recreated from the positions of these words in this list using the sequence 1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5

I've done the first part of the task and so far I have this code:

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR 
YOUR COUNTRY"
s = sentence.split()
positions = [s.index(x)+1 for x in s]

my_list = ['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 
'YOU', 'ASK', 'WHAT', 'YOU', 'CAN', 'DO', 'FOR', 'YOUR', 'COUNTRY']
unique = []
[unique.append(item) for item in my_list if item not in unique]

print("The sentence",sentence,"contains the words",unique)

print("It can be recreated from the positions of the words in this 
list",s,"using the sequence",positions)

Is this considered efficient? How can I make it more efficient?

However, my problem is the rest of the task:

Save the list of words and the positions of these words in the sentence as separate files or as a single file.

I have no idea what it means. I did some research and I found out that it involves reading and writing to a file, but I don't have a clue what to do next.

Nevertheless I came up with this:

my_list = ['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 
'YOU', 'ASK', 'WHAT', 'YOU', 'CAN', 'DO', 'FOR', 'YOUR', 'COUNTRY']
with open("task2.txt", 'w') as f:
f.write("\n".join(map(str, my_list)))

But I don't know what it does or how it relates to the task??? I need to be able to explain my code which I can't do since I don't understand it.

Can someone please fix my code? Thank you

3 Answers3

2

Since you've tagged this R, here's a solution in R.

sentence <- "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"

find.words <- strsplit(sentence, " ")[[1]] # split string into 
words <- unique(find.words) # find unique words
# find positions of words in the unique vector
words.pos <- sapply(find.words, FUN = function(x, words) which(x == words), words = words)

sprintf("The sentence can be recreated from the positions of these words in the list %s using the sequence %s", 
        paste(words, collapse = " "), paste(words.pos, collapse = " "))

[1] "The sentence can be recreated from the positions of these words in the list ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU using the sequence 1 2 3 4 5 6 7 8 9 1 3 9 6 7 8 4 5"

Here is one way of how you could write it to a file.

# find position by word
pos.by.word <- sapply(words, FUN = function(x, fw) which(x == fw), fw = find.words)
# paste names together
concat.words.freq <- mapply(FUN = function(x, y) {
  paste(x, paste(y, collapse = " "))
}, names(pos.by.word), pos.by.word)

write.table(as.data.frame(concat.words.freq), file = "out.txt",
            row.names = FALSE, col.names = FALSE, quote = FALSE)

And out.txt looks like this:

ASK 1 10
NOT 2
WHAT 3 11
YOUR 4 16
COUNTRY 5 17
CAN 6 13
DO 7 14
FOR 8 15
YOU 9 12
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
0

I give you a possible solution:

from collections import defaultdict

words = defaultdict(list)
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s = sentence.split()
for pos, word in enumerate(s):
    words[word].append(pos + 1)

Output:

In [20]: words
Out[20]: 
defaultdict(list,
        {'ASK': [1, 10],
         'CAN': [6, 13],
         'COUNTRY': [5, 17],
         'DO': [7, 14],
         'FOR': [8, 15],
         'NOT': [2],
         'WHAT': [3, 11],
         'YOU': [9, 12],
         'YOUR': [4, 16]})
Diego Navarro
  • 9,316
  • 3
  • 26
  • 33
-1

Try this

s = sentence.split()
positions = [s.index(x)+1 for x in s]

map = {}
for w in s:
  map[w] = True

words = []
for w in map:
  words.append(w)

print(words)
John London
  • 1,250
  • 2
  • 14
  • 32