Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'd like to write a small program in Python that takes a hebrew text, converts each letter into its ghematria numbers, sum the numbers in each word, then prints the words as numbers.
to start off I was thinking to set the aleph beth variables,
like:
א=1
ב=2
ג=3
ד=4
and so on...
Keep in mind that since Hebrew letters are strong right-to-left characters, everything starts showing up right-to-left after one. Those assignments just look backward; they really are valid in Python 3.
Now that I have set the variables I was wondering how could I get the numbers for all the words from a txt file (unicode 8 of course)..
any help for a noob?
Keep in mind that since we are using hebrew the value of the variable is set at the LEFT of the = sign... and not at the right...
Python doesn't care. 1 = א won't work anway as you are trying to assign a value to an int.
The other way around also won't be a pleasure to work with. Unicode identifiers are supported only by Python 3, but I wouldn't go down that road.
What I would do is have a dictionary with letters as keys and numbers as values.
Then simply read the text file, convert each letter to its number and sum.
EDIT Something of this sort.
Note that I only have 3 letters in the dictionary because I'm lazy, and that I'm
using the dictionary's get() method so I won't get KeyError on spaces, new lines and punctuation marks.
vals = {'א': 1,
'ב': 2,
'ג': 3}
with open(path_to_file, encoding='utf-8') as f:
text = f.read()
sum = 0
for letter in text:
sum += vals.get(letter, 0)
print(sum)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I am working on a coding challenge that takes user input removes matching letters that come after a comma and scrubs the white space. For the life of me I can’t figure it out.
For example the user inputs:
Hello World, wea
Output would be:
Hllo orld
Any direction on how to solve this would be greatly appreciated it is driving me crazy.
Try using a simple for loop that iterates across the phrase and places characters that don't appear after the comma into a separate string. Then the separate string is the result once the for loop has finished.
There are tons of different ways of achieving this, this way is fairly easy to understand though.
text = "Hello World, wea"
phrase, chars = text.split(",") # split the text by the comma
chars = chars.strip() # remove whitespace from second part
output = "" # separate string to collect chars
for letter in phrase:
if letter.lower() not in chars: # check lowercased letters
output += letter
print(output)
output
Hllo orld
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Any tips on how to count the amount of characters in each line of a text file, to then compare them using python?
It would be helpful to have an idea of what the end goal of your code is. What information do you want to gain from comparing the number of characters on a line? I would have written this as a comment, but it's not yet an option for me since I just joined.
If you're completely lost and don't know where to begin, here are some general bits of code to get you started (this is using Python 3.x):
file = open("YourFileName.txt", "r")
stringList = file.readlines()
The first line will open (read, hence the "r") the file in question. The second line of code goes through each line in the file and assigns them to a variable I called stringList. stringList is now a list, in which each element is a string corresponding to one line of your text file.
So,
print(stringList)
should return
['line0', 'line1', 'line2', 'line3', etc...]
It's possible that stringList could look like
['line0\n', 'line1\n', 'line2\n', 'line3\n', etc...]
depending on how your file is formatted. In case you didn't know, the '\n' is a newline character, equivalent to hitting enter on the keyboard.
From there you can create another list to hold the length of each line, and then loop through each element of stringList to store the lengths.
lengthList = []
for line in stringList:
lengthList.append(len(line))
len(line) takes the number of characters in a string and converts it to the equivalent integer value. Your lengthList will then contain how many characters are on each line, stored as ints. If there are '\n's, you may want to use len(line) - 1, depending on what you want to do with the lengths.
I hope this is helpful; I can't help with the comparisons until you provide some code and explain more specifically what you want to accomplish.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a program that
Takes a text file with several sentences
Asks the user if they want to compress/decompress the file.
If Compress is chosen, the sentence will have all the unique words and the positions of these words needed to recreate the sentence again.
If decompress is chosen, the compressed text file will need to be found and using the position list given and the unique words - decompressed - the several sentences in the files need to be on separate lines.
Here is the code I have managed to create. It is a subroutine and it's rather faulty.
uniqueWords = []
positions = []
file =
def valChoice():
choice = (" ")
while choice not in ["compress", "decompress"]:
choice = input("Choose compress or decompress").lower()
if choice not in ["compress", "decompress"]:
print("Please input compress or decompress")
finalChoice = valChoice()
if finalChoice = ("compress"):
print("This where i get confused..")
elif finalChoice = ("decompress"):
print("This where i get confused..")
What is wrong with this code? How can I fix it?
With my caveat above, I'll take a shot at what I think you're asking.
To compress the file, iterate through the input words. Store each word reference in a dictionary: the word itself is the key, and its position is the value. If the word is already in the dictionary, then add the new position reference to the existing list of references.
Decompression works in reverse: make a sequence of positions and words. Sort that sequence into ascending order. Concatenate the words to make the original text.
Is that the level of help you need right now?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to check a TSV of results and want to see how many times F and S come up (for failure or success) however I'm not sure how to doing this counting, or how to having it search the file
I've come across something that is sort of what I'm looking for: Python Program how to count number of times the letter c shows up
def countletter(S, F):
count = 0
for x in S:
if x.lower() == F.lower():
count += 1
return count
print (countletter("abcdefFf","F"))
But it isn't perfect and I'm not sure how to make it search the file.
Assuming that the count result applies to the whole file you can use a collections.Counter:
from collections import Counter
with open('input.tsv') as infile:
counts = Counter(infile.read())
for c in 'SF':
print '{}: {}'.format(c, counts.get(c))
This has the advantage of allowing you to obtain counts of any character (not just "S" and "F") with one pass of the file.
You could also just use str.count() for a specific character (or a string), but if you need counts more than one character you'll find a Counter more convenient and probably faster too.
You need to pass the file contents to your countletter function.
with open("FILE_TO_OPEN_AND_READ.txt") as f:
data = f.read()
print (countletter(data,"F"))
This opens and reads the file into data. For this example, I'm assuming your file is relatively small. Then data is passed into countletter as the first parameter, instead of a hardcoded string ("abcdefFf" in your example).
One note about your code, you are missing a closing parenthesis in your print statement. I've added that in my example above.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Basically, I'm working on a Python fill in the letters type of game (kind of like Hangman).
The problem is I can't seem to get the program to record duplicate points.
What I mean is:
The program asks the user for a word. That word, let's say....football, is converted into a masked string (ex. **)
Then it continually asks the user for letter inputs. Let's say the user enters:
f
o
t
b
a
l
And then it fills out the word. For each letter that is guessed correctly, the user is awarded ONE point. But the problem is that for a word like football,only 6 points are awarded because some of the letters are duplicates.
Basically, the way I've coded it, each time a correct letter is guessed, another point is added on top of the overall total points. Is there a better way of doing this that can include the duplicate letters?
You could perhaps use count() on the word to see how many times the letter is in the word:
word = 'football'
# Code here to take input
# if input is in word:
points = word.count(the_input)
award_player(points)
You could try a list comprehension combined with sum():
>>> s = "foot**ll"
>>> sum([1 for x in s if x != '*'])
6