Python Forum
Need help with a code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with a code
#1
Hey everyone,
I have a problem solving this code, so can you help?

The problem is: to do a repetition count and give the result as a dictionary. And also if the letter in the word repeats itself, it should print it out.

Idea any ideas?? Huh
Reply
#2
Welcome to the forums. What have you tried to solve it? You need to show us your code (in python tags). If you get any errors - post the full traceback in error tags. Ask specific question(s) when you get stuck. We are glad to help, but you need to put some effort.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Aug-06-2020, 10:57 AM)saratha Wrote: Hey everyone,
I have a problem solving this code, so can you help?

The problem is: to do a repetition count and give the result as a dictionary. And also if the letter in the word repeats itself, it should print it out.

Idea any ideas?? Huh
I could help you, but you haven't tried anything. Show us the piece of code, and we'll be able to help you out.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
This is a really simple task. You think it is hard because you don't know how to write the program. Yet.

Start with solving the problem, not writing the program. I give you a piece of paper and a pencil. I tell you to write down the word "abracadabra".

Tell me all the letters that appear in the word.
Tell me how many times each letter appears in the word.

Were you able to accomplish the task? Was it hard?

Now translate the algorithm you used into a Python program. What was the first thing you did?

Step 1. Write down the word.
How do you enter data, like words and numbers, into a Python program? Are you going to get the word from a file or does the user type the word at the keyboard. Solve this problem first and verify you get the word by printing it out. Testing the code you wrote to verify it works is "unit testing".

Step 2. Make a list of the letters. How did you do that step with pencil and paper? The assignment says your result should be a dictionary. Find out how dictionaries work and what is the dictionary equivalent of "making a list of the letters". Print the dictionary and verify it contains all the letters that appear in the word. Another unit test!

Step 3: Count how many times each letter appears. How did you do that with pencil and paper. Dictionaries map a value to a key. You are interested in letters and counts. Which do you think is the best choice for a key? When a letter appears more than once, how do you record that information in your dictionary? When this step is complete print the dictionary again to verify the letter counts are correct. More unit testing!

Step 4: Print each letter that repeats itself. What does this mean? Does that mean letters that appear more than once in the word? We have a dictionary of letters and the number of times each letter appears. How can that be used to solve this last step.

See, not that bad. Only 4 little steps.

Step 4:
Reply
#5
Whenever you get stuck getting started on something like this it's a good idea to write out the steps in plain language and then convert it into python. This is called pseudo-code and it separates the logic from the syntax and lets you focus on just the logic. In your case it would look something like this:

Define a letter_counter function that takes in a string parameter called word_in:
__Create an empty dictionary to hold the results called result
__Loop through every letter in word_in:
____If letter is in the keys of the result dictionary:
______Add 1 to that key's value
____If not:
______Create a new key called letter and set its value to 1
__Return the result dictionary to end the function

Now you can look over the pseudo-code and start picking out the variables and functions you will need to translate it into python.

Note that I am creating a function since it's good programming. If you haven't learned functions yet just ignore the first and last line of the pseudo-code.

See how far you can get and, of course, read up on python dictionary methods if you haven't already. Wink
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#6
(Aug-07-2020, 05:45 PM)deanhystad Wrote: This is a really simple task. You think it is hard because you don't know how to write the program. Yet.

Start with solving the problem, not writing the program. I give you a piece of paper and a pencil. I tell you to write down the word "abracadabra".

Tell me all the letters that appear in the word.
Tell me how many times each letter appears in the word.

Were you able to accomplish the task? Was it hard?

Now translate the algorithm you used into a Python program. What was the first thing you did?

Step 1. Write down the word.
How do you enter data, like words and numbers, into a Python program? Are you going to get the word from a file or does the user type the word at the keyboard. Solve this problem first and verify you get the word by printing it out. Testing the code you wrote to verify it works is "unit testing".

Step 2. Make a list of the letters. How did you do that step with pencil and paper? The assignment says your result should be a dictionary. Find out how dictionaries work and what is the dictionary equivalent of "making a list of the letters". Print the dictionary and verify it contains all the letters that appear in the word. Another unit test!

Step 3: Count how many times each letter appears. How did you do that with pencil and paper. Dictionaries map a value to a key. You are interested in letters and counts. Which do you think is the best choice for a key? When a letter appears more than once, how do you record that information in your dictionary? When this step is complete print the dictionary again to verify the letter counts are correct. More unit testing!

Step 4: Print each letter that repeats itself. What does this mean? Does that mean letters that appear more than once in the word? We have a dictionary of letters and the number of times each letter appears. How can that be used to solve this last step.

See, not that bad. Only 4 little steps.

Step 4:
thanks a lot ! it helped me :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020