***PYTHON CODING QUESTION*** Hello, I'm having trouble figuring out which hidden tests have made my code invalid. Here i
Posted: Fri Apr 29, 2022 6:41 am
***PYTHON CODING QUESTION***
Hello, I'm having trouble figuring out which hidden tests have
made my code invalid.
Here is the question and test codes:
Here is my code:
And here was the error I got:
There must be another way to write a code to this question,
given that the one I've gone for isn't passing all of the
tests!
Please help, it would be much appreciated <3
N-gram is a list of lists, where an inner list is a group of n consecutive words, and the outer list contains those groups for a given sentence. For example, the sentence this is a long sentence' with n=3 will make: [['this', 'is', 'a'], ['is', 'a', 'long'], ['a', 'long', 'sentence']] As above, the inner list is 3 consecutive words, and the outer list is all those 3-gram words generated from the example sentence. For this task, write a function construct_ngrams (sentence, n) which takes input parameters sentence (type string) and n (type integer), and returns a list that contains N-gram generated from the given sentence. If no such N-gram could be generated (think about the cases), then it simply returns an empty list. Note: Although a more elegant solution could be generated using the zip function, you cannot use that for this question (as we are practising primitive Python coding skills). For example: Test Result ngrams = construct_ngrams ('this is a long sentence', 3) [['this', 'is', 'a'], ['is', 'a' print(ngrams) ngrams = construct_ngrams ('this is a long sentence', 6) [] print(ngrams)
4 7 1 def construct_ngrams (sentence, n): 2 """rearranges a sentence n amount of times" 3 nngrams = [] wordlings = sentence.split() 5 if len(wordlings)<n: 6 return [] 7 for i in range(len(wordlings) - n + 1): ng = [] 9 for j in range (i, i + n): 10 ng.append(wordlings[j]) 11 nngrams.append(ng) 12 return nngrams 8 PPP
Test Expected [['this', ngrams = construct_ngrams ('this is a long sentence', 3) print(ngrams) [] ngrams = construct_ngrams ('this is a long sentence', 6) print(ngrams) ngrams = construct_ngrams ('this is another long sentence for testing', 4) [['this', print(ngrams) ngrams = construct_ngrams ('this is another long sentence for testing', 6) [['this', print(ngrams) Your code failed one or more hidden tests. Your code must pass all tests to earn any marks. Try again.
Hello, I'm having trouble figuring out which hidden tests have
made my code invalid.
Here is the question and test codes:
Here is my code:
And here was the error I got:
There must be another way to write a code to this question,
given that the one I've gone for isn't passing all of the
tests!
Please help, it would be much appreciated <3
N-gram is a list of lists, where an inner list is a group of n consecutive words, and the outer list contains those groups for a given sentence. For example, the sentence this is a long sentence' with n=3 will make: [['this', 'is', 'a'], ['is', 'a', 'long'], ['a', 'long', 'sentence']] As above, the inner list is 3 consecutive words, and the outer list is all those 3-gram words generated from the example sentence. For this task, write a function construct_ngrams (sentence, n) which takes input parameters sentence (type string) and n (type integer), and returns a list that contains N-gram generated from the given sentence. If no such N-gram could be generated (think about the cases), then it simply returns an empty list. Note: Although a more elegant solution could be generated using the zip function, you cannot use that for this question (as we are practising primitive Python coding skills). For example: Test Result ngrams = construct_ngrams ('this is a long sentence', 3) [['this', 'is', 'a'], ['is', 'a' print(ngrams) ngrams = construct_ngrams ('this is a long sentence', 6) [] print(ngrams)
4 7 1 def construct_ngrams (sentence, n): 2 """rearranges a sentence n amount of times" 3 nngrams = [] wordlings = sentence.split() 5 if len(wordlings)<n: 6 return [] 7 for i in range(len(wordlings) - n + 1): ng = [] 9 for j in range (i, i + n): 10 ng.append(wordlings[j]) 11 nngrams.append(ng) 12 return nngrams 8 PPP
Test Expected [['this', ngrams = construct_ngrams ('this is a long sentence', 3) print(ngrams) [] ngrams = construct_ngrams ('this is a long sentence', 6) print(ngrams) ngrams = construct_ngrams ('this is another long sentence for testing', 4) [['this', print(ngrams) ngrams = construct_ngrams ('this is another long sentence for testing', 6) [['this', print(ngrams) Your code failed one or more hidden tests. Your code must pass all tests to earn any marks. Try again.