python coding challenge

Watch this thread
veish
Badges: 3
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Report Thread starter 8 months ago
#1
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.
0
reply
Composure
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report 8 months ago
#2
Have you at least attempted to code it?
0
reply
veish
Badges: 3
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
Report Thread starter 8 months ago
#3
(Original post by Composure)
Have you at least attempted to code it?
yh but i couldnt get the answer during class
0
reply
veish
Badges: 3
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Report Thread starter 8 months ago
#4
(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
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
Report 8 months ago
#5
(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?
0
reply
Composure
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#6
Report 8 months ago
#6
(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.
Last edited by Composure; 8 months ago
0
reply
veish
Badges: 3
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#7
Report Thread starter 8 months ago
#7
(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
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#8
Report 8 months ago
#8
(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.
0
reply
Composure
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#9
Report 8 months ago
#9
(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 ¯\_(ツ)_/¯
Last edited by Composure; 8 months ago
1
reply
veish
Badges: 3
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#10
Report Thread starter 8 months ago
#10
(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
0
reply
mqb2766
Badges: 19
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#11
Report 8 months ago
#11
(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.
0
reply
Composure
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#12
Report 8 months ago
#12
(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")).
Last edited by Composure; 8 months ago
0
reply
PossiblyNotGod
Badges: 20
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#13
Report 8 months ago
#13
so the code would be something like:


Code:
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[i])
Last edited by PossiblyNotGod; 8 months ago
0
reply
CompSciJP
Badges: 14
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#14
Report 8 months ago
#14
(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:
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest

How did The Student Room help you with your university application?

Talking to current university students (8)
23.53%
Talking to peers going through the same thing (11)
32.35%
Speaking to student ambassadors from the universities (2)
5.88%
Speaking to staff members from universities (0)
0%
Using the personal statement builder, library or helper service (3)
8.82%
Reading articles about what steps to take (6)
17.65%
Learning about/speaking to Student Finance England (2)
5.88%
Something else (tell us in the thread) (2)
5.88%

Watched Threads

View All