Python : how to decode the text in s-language (strings) - python

Input exists out of a sentence or text in s-language: Isis thasat yousour sisisteser?
Output exists out same sentence without s-language : Is that your sister?
Problems:
I have the following code but some things are not working. For example I can not append after the if statements and my if statements are too litteral. Also the print(decoded_tekst) is not working.
Method:
I iterate through the different positions of the text with two variables ("vowelgroup" to store the vowels and "decoded text" to store the consonant and if "s" to replace it by the vowelgroup).
text = input('Enter a text: ')
vowelgroup = []
decoded_text = []
sentence = []
vowel = 'aeiou'
count = 0
for i in text:
if i is not vowel and not "s":
sentence = decoded_text.append(i)
if i is vowel:
vowelgroup = vowelgroup.append(vowel)
if i is "s":
decoded_text = sentence.append(vowelgroup)
count += 1
print(decoded_text)

You look a little confused about how append works. append just adds the argument to the end of the list and it returns None. E.g:
l = [1,2]
a = l.append(3)
print a # None
print l # [1,2,3]
So you just use it like:
l = [1,2]
l.append(3)
print l # [1,2,3]

Related

python i need to not repeated words in a while loop

I need not to get the words again once its displayed using random.choice,
this is the problem it shouldnt repeated words because the words have been already used
I tried to delete the word using list.remove but It doesn't work and i didn't get any results
import random
a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
c = True
while c:
c = input("")
filtered_list = []
for word in a:
if c.upper() in word:
filtered_list.append(word)
if len(filtered_list) > 0:
words = random.choice(filtered_list)
print(words)
else:
print(f"there are any words with {c} or all the words have been used")
i'm not sure the original message of the example to solve; but i tried this
#!/usr/bin/python3
import random
a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
c = True
while c:
c = input("")
print("a ", a)
filtered_list = []
for word in a:
if c.lower() in word:
a.remove(word)
filtered_list.append(word)
if len(filtered_list) > 0:
words = random.choice(filtered_list)
print(words)
else:
print(f"there are any words with {c} or all the words have been used")
Use another list for used words:
from random import choice
a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
used = []
while c := input('').lower():
filtered_list = [word for word in a if c in word and word not in used]
if filtered_list: used += [words := choice(filtered_list)]; print(words)
else: print(f'there are any words with {c} or all the words have been used'); # used = []
Output:
e
ocean
e
blondie
e
threw
e
dime
e
theme
e
hello
e
there are any words with e or all the words have been used
You might also need to reset used list to empty list after reach else statement for different search input of another character or letter.
You should start by creating a new normalised list because this code is destructive. In the question, the list contains all lowercase words but it's better to generalise
Then:
from random import choice
a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
acopy = [word.lower() for word in a]
while acopy:
if (c := input('Enter a single character: ').lower()) == '?':
print(acopy)
elif len(c) == 1:
if possibles := [i for i, word in enumerate(acopy) if c in word]:
print(acopy.pop(choice(possibles)))
else:
print(f'There are no words containing {c}')
As written, the while loop will only terminate when the acopy list is empty. You might want to do something else such as empty input.
Bonus: Enter ? to see what's remaining in the list
If i understand your problem correctly, your requirement is once the word displayed, it will not displayed again. So delete the word from the list a once you get the value from random.choice , so that you won't get the word again or maintain another list which holds the value already shown
import random
a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
already_show = []
c = True
while c:
c = input("Enter Letter to search:")
filtered_list = []
for word in a:
if c in word:
filtered_list.append(word)
if len(filtered_list) > 0:
words = random.choice(filtered_list)
if words not in already_show:
already_show.append(words)
print(words)
else:
print("random.choice selected the already selected word")
already_show = [] #Reset for another search
else:
print(f"there are any words with {c} or all the words have been used")

How do I input multiple arguments into strip() function?

today I was tasked to create a program that takes a user input and prints out the vowels and constants of what the user input was. I thought I was doing good so far but then I got an error while trying to use strip(). The error said the max arguments it could take were 1 and I was putting in multiple. How should I go about this?
lst1 = ['a','e','i','o','u']
lst2 = ['b','c','d','f','g','j','k','l','m','n','p','q','s','t','v','x','z','h','r','w','y']
lst3 = [] ### this is for vowels
lst4 = [] ### this is for userinput
lst5 = [] ### this is for constants
def vowelstrip(lst4):
maxlength = 1
while len(lst4) < maxlength:
lst4 = input('Enter a line of text: ')
lst3 = lst4.strip('b','c','d','f','g','j','k','l','m','n','p','q','s','t','v','x','z','h','r','w','y')
lst5 = lst4.strip('a','e','i','o','u')
print(f'vowels are: {lst3}\n Constants are: {lst5}')
You can have strip remove multiple characters by specifying them as a single string (not multiple arguments), but it only removes characters from the beginning and end of a string, not the middle, so it's not really suitable for what you're trying to do:
>>> "foobar".strip("rf")
'ooba'
>>> "foobar".strip("aeiou")
'foobar'
I'd suggest using a generator expression and join to build a new string by iterating over the user's input:
vowels = 'aeiou'
def extract_vowels(text: str) -> str:
return ''.join(c for c in text if c in vowels)
def extract_consonants(text: str) -> str:
return ''.join(c for c in text if c.isalpha() and c not in vowels)
text = input('Enter a line of text: ')
print(f'vowels are: {extract_vowels(text)}')
print(f'consonants are: {extract_consonants(text)}')
Enter a line of text: the quick brown fox
vowels are: euioo
consonants are: thqckbrwnfx

Python : the s language decoded using append

Input a sentence: Whasat doso yousou presefeser?
Output a sentence: What do you prefer?
At the moment only the p is changed by []. There are some mistakes with the code but I can not find it.
# input a sentence
text = input('Enter a text: ')
# create two variables
vowelgroup = []
decoded_text = []
vowel = 'aeiou'
# loop through all letters of sentence
for i in text:
if i != "vowel" and i != "s":
decoded_text.append(i)
if i == "vowel":
vowelgroup.append(i)
if i == "s":
decoded_text.append(vowelgroup)
# unlist the decoded tekst to string
L = ''.join(map(str, decoded_text))
print(L)
# input a sentence
text = input('Enter a text: ')
# create two variables
vowelgroup = []
decoded_text = []
vowel = 'aeiou'
# loop through all letters of sentence
container = False
for i in text:
if container:
container = False
continue
if i != "vowel" and i != "s":
decoded_text.append(i)
if i == "vowel":
vowelgroup.append(i)
if i == "s":
for j in vowelgroup:
decoded_text.append(j)
container = True
# unlist the decoded tekst to string
L = ''.join(map(str, decoded_text))
print(L)
Is this what you are aiming for? I don't get which among the letters beside an 's' will should be the trigger. In this case, once an 's' is scanned, the next character will automatically be skipped.
We iterate through the sentence letter by letter i. When the first letter i is not a vowel (a consonant) we store it in our decoded text variable. We repeat till when we encounter a vowel we save this vowel letter(s) in the vowel group till we encounter a 's' and we want to pass this vowelgroup to the decoded tekst instead of the 's'. Than we continue till we encounter again a non vowel (consonant) letter which will be added again to the decoded tekst and so on and on for each letter i.
text = input('Enter a text: ')
vowelgroup = []
decoded_text = []
vowel = 'aeiou'
for i in text:
if i != 'vowel':
decoded_text.append(i)
if i == 'vowel':
vowelgroup.append(i)
if i == 's':
decoded_text.append(vowelgroup)
L = ''.join(map(str, decoded_text))
print(L)

Iterating through a dictionary to find the most frequently used word

I made a quick bit of code that checks a list of input words by the user and counts how many of the same word the user has typed. if print is input it is supposed to print the most frequent word used as well as how many times it was used for example
The word 'Hello' has been used '12' times
i am just not sure how to get it to iterate through the dictionary and find the most frequent word
Here is the code
d = {}
L2 = []
Counter = 0
checker = -1
while True:
Text = str(input('enter word '))
Text = Text.lower()
if Text in ['q', 'Q']:
break
if Text in ['Print', 'print']:
for word in L2:
if word not in d:
counter = L2.count(word)
d[word] = counter
#This part does not work
for value in d[0]:
if checker < value:
checker = value
print(checker)
#This part does
L2.append(Text)
d = {}
L2 = []
while True:
text_input = input('enter word ') # input by default stores values in string format
text_input = text_input.lower()
if text_input == 'q': # you have used text_input.lower() so you don't need to use it is 'q' or 'Q' it will never be 'Q'
break
elif text_input == 'print':
for item in set(L2): # set(L2) will only keep unique values from list L2
d[item] = L2.count(item) # counts for all unique items and append it to dictionary
for word, count in d.items(): # by using this loop you can print all possible max values i.e. if two word has same max occurance it will print both
if count == max(d.values()):
print("Word : {} repeated {} times.".format(word, count))
else:
L2.append(text_input) # input is neither 'q' nor 'print' hence append word in list

Goes to a file and prints only the words that don't have any digits in them

Also I need to make the program show the words that DO have digits in thee digits in them SEPERATELY
f = open("lolpa.txt", "r")
list1 = (f)
temp = []
for item in list1:
if "1" "2" "3" "4" "5" "6" "7" "8" "9" "0" in item:
temp.append(item)
else:
print(item)
is what i have so far, but it for some reason shows all the words.
EDIT: The lolpa.txt is just a file for comparison
EDIT: If that would change anything , I'm using python 3.2.
Something like this will get you started, question isn't very clear.
with open("lolpa.txt") as f:
for word in f.readline().split(): # assuming all words are on the first line
digits = [c for c in word if c.isdigit()]
if digits: # digits list is not empty
print(' '.join(digits)) # shows digits with space in between
else:
print(word) # prints word normally
The following will put whole words having numbers in them in one file and without numbers in another.
f = open('lolpa.txt', 'r')
d = open('lolpa_with_digit.txt', 'w')
nd = open('lolpa_without_digit.txt', 'w')
rowlist = (f)
temp = []
for line in rowlist:
words = line.split(' ')
for word in words:
word = word.strip()
has_digit = False
for char in word:
if char.isdigit():
has_digit = True
if has_digit:
d.write(word + '\n')
else:
nd.write(word +'\n')
I'm no regular expression expert but this should work(of course you'd have to loop over each line yadayada):
import re
prog = re.comile(r'\b[a-zA-Z]+\b')
result = prog.findall(your_line_here)
result would then be a list of all the words

Categories