The Student Room Group

python coding challenge

I received this challenge at schl and I'm not sure abt how to answer it

---------------------------------------------------------------------------------------------------------

Digit Words

A digit word is a word where, after possibly removing some letters, you are left with one of the single digits: ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT or NINE.
For example:

BOUNCE and ANNOUNCE are digit words, since they contain the digit ONE.
ENCODE is not a digit word, even though it contains an O, N and E, since they are not in order.

Write a program which reads in a single upper-case word (with at most fifteen letters) and determines if it is a digit word.

If the word is not a digit word you should output the word NO.
If the word is a digit word you should output the digit it contains, as a number.

We will not be testing any words which contain more than one digit.
Have you at least attempted to code it?
Reply 2
Original post by Composure
Have you at least attempted to code it?

yh but i couldnt get the answer during class
Reply 3
Original post by veish
yh but i couldnt get the answer during class

i just wanna see how others would attempt this problem so that i can learn from it
Reply 4
Original post by veish
i just wanna see how others would attempt this problem so that i can learn from it

Why not post what you've tried?
Original post by veish
yh but i couldnt get the answer during class

To push you in the right direction, firstly you're going to want a container of some sort to store the numerals as strings (numerals being "ONE", "TWO", "THREE" etc). The container can be a list, tuple, or dictionary. I would recommend a dictionary where the keys are the aforementioned strings, and the values are their integer value.
(edited 2 years ago)
Reply 6
Original post by mqb2766
Why not post what you've tried?

im really new to python so my code is very simple and it doesnt work at all but here it is

def dw():
message = input("what is your word: ")
for char in message:
if char == "O" and char== "N" and char== "E" :
print ("one")




dw()


i was trying to make the first part work and then once i got that i could copy paste for the rest of the code just changing the letters
Reply 7
Original post by veish
im really new to python so my code is very simple and it doesnt work at all but here it is

def dw():
message = input("what is your word: ")
for char in message:
if char == "O" and char== "N" and char== "E" :
print ("one")




dw()


i was trying to make the first part work and then once i got that i could copy paste for the rest of the code just changing the letters

So what do you think doesn't work? If it runs, have you tried it with
ONE

You could print out
char
each time round the loop to understand what your test means, or use the debugger.
Original post by veish
im really new to python so my code is very simple and it doesnt work at all but here it is

def dw():
message = input("what is your word: ")
for char in message:
if char == "O" and char== "N" and char== "E" :
print ("one")




dw()


i was trying to make the first part work and then once i got that i could copy paste for the rest of the code just changing the letters

If this is the level you're at then I don't think there's a real point in attempting the question as it's way above what a beginner would be capable of.

The solution requires knowledge of mutability, indexing, arguments, parameters, exception raising, containers, iterables, iterators, standard library modules, boolean logic, conditions, and returns. If your teacher hasn't covered these then there's not much you can do ¯\_(ツ)_/¯
(edited 2 years ago)
Reply 9
Original post by Composure
If this is the level you're at then I don't think there's a real point in attempting the question as it's way above what a beginner would be capable of.

The solution requires knowledge of mutability, indexing, arguments, parameters, exception raising, containers, iterables, iterators, standard library modules, boolean logic, conditions, and returns. If your teacher hasn't covered these then there's not much you can do ¯\_(ツ)_/¯

oh ok thanks anyways
Original post by veish
oh ok thanks anyways

The question is very similar to an old BIO (british informatics olympiad) question, so it is tough if you're just beginning. However, if you are learning, you could write a program which checked whether only ONE appeared in the word. If you got that working you could then see if you could extend it to also check for TWO, etc. Youd learn something from it, even if it didnt solve the problem fully.
Original post by veish
oh ok thanks anyways

As an alternative, I would recommend going through some of the tutorials and challenges (the ones under "Very Easy" and "Easy") on Edabit. If you solve a challenge you can look at other people's answers and learn from them.

To give some feedback on your code from earlier:
Original post by veish
Line 1. def dw():

This is a confusing function name. Rarely do you want to use abbreviations when naming things. I don't think even digit_words() would be a good name. I would call it something a bit more descriptive like digit_occurrence().
Original post by veish
Line 2. message = input("what is your word: ")

Not necessary to use the input() function. Remove this line and just add a parameter to the previous line. E.g. def digit_occurrence(word):. Then you can call the function with something like digit_occurrence("ANNOUNCE").
Original post by veish
Line 3. for char in message:

I haven't thought too deep into it but I don't think you'll find an elegant solution if you start by iterating over the word being checked.
Original post by veish
Line 4. if char == "O" and char== "N" and char== "E" :

Your logic is off here. The comparisons are mutually exclusive with eachother, char can't possibly equal "O", "N", and "E" all at once. So this condition will never resolve to True.
Original post by veish
Line 5. print("one")

As the condition on the previous line will never resolve to True; this print() won't ever be executed. Even then you shouldn't be using print() inside the function. Functions are much more useful when they return something. If you want to see the result of a function call then put the function call itself inside a print(). E.g. print(digit_occurrence("ANNOUNCE")).
(edited 2 years ago)
so the code would be something like:



words_list = [[O,N,E],[T,W,O],[T,H,R,E,E]...]
inputted_word = input()

def get_answer():
word_is_digit_word = False
i = 0
for digit_word in words_list:
order_iter = 0
for letter in inputted_word:
if letter == digit_word[order_iter]:
if order_iter < len(digit_word):
order_iter += 1
else:
word_is_digit_word = True
return word_is_digit_word, i
i += 1
return word_is_digit_word, i


word_is_digit_word, i = get_answer()

print('was it a digit word?: ', word_is_digit_word)
if word_is_digit_word:
print('which word was it?: , words_list)
(edited 2 years ago)
Original post by veish
I received this challenge at schl and I'm not sure abt how to answer it

---------------------------------------------------------------------------------------------------------

Digit Words

A digit word is a word where, after possibly removing some letters, you are left with one of the single digits: ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT or NINE.
For example:

BOUNCE and ANNOUNCE are digit words, since they contain the digit ONE.
ENCODE is not a digit word, even though it contains an O, N and E, since they are not in order.

Write a program which reads in a single upper-case word (with at most fifteen letters) and determines if it is a digit word.

If the word is not a digit word you should output the word NO.
If the word is a digit word you should output the digit it contains, as a number.

We will not be testing any words which contain more than one digit.

I remember this from a programming Olympiad a few years back now :innocent:

Break the question down, design the code and then implement your design. "Best by test" :flute:

Quick Reply

Latest

Trending

Trending