I need to convert a string word where each character that appears only once should be appear as '(' in the new string. Any duplicate characters in the original string should be replaced with ')'.
My code below...
def duplicate_encode(word):
new_word = ''
for char in word:
if len(char) > 1:
new_word += ')'
else:
new_word += '('
return new_word
The test I'm not passing is as follows:
'((((((' should equal '()()()'
This would suggest that, if for example, the input is "recede," the output should read ()()().
Your Code is Good just need some alteration it will be great.
def duplicate_encode(word):
"""
To replace the duplicate letter with ")" in a string.
if given letter is unique it replaced with "("
"""
word_dict = {} # initialize a dictionary
new_word = ""
for i in set(word): # this loop is used to count duplicate words
word_count = word.count(i)
word_dict[i] = word_count # add letter and count of the letter to dictionary
for i in word:
if word_dict[i] > 1:
new_word += ")"
else:
new_word += "("
print new_word
duplicate_encode("recede")
I think you got the answer :)
Just because (it's late and) it's possible:
def duplicate_encode(word):
return (lambda w: ''.join(('(', ')')[c in w[:i] + w[i+1:]] for i, c in enumerate(w)))(word.lower())
print(duplicate_encode("rEcede"))
OUTPUT
> python3 test.py
()()()
>
Seems like your result is based on the number of occurrences of a character in the word, you can use Counter to keep track of that:
def duplicate_encode(word):
from collections import Counter
word = word.lower() # to disregard case
counter = Counter(word)
new_word = ''
for char in word:
if counter[char] > 1: # if the character appears more than once in the word
# translate it to )
new_word += ')'
else:
new_word += '('
return new_word
duplicate_encode('recede')
# '()()()'
Related
I'm trying to add the letter 'X' before each vowel in a string or sentence, however, when there is a repeated vowel, the letter 'X' should only be written once. For example, the word 'speeding' should look like this 'spXeedXing' but i'm getting 'spXeXedXing'.
I know why I'm getting this problem but don't know where to go from here to make it work.
Code below
def vowels(string):
newString = ""
for letter in string:
if letter in "aeiou":
newString += "X" + letter
else:
newString += letter
print(newString)
if __name__ == "__main__":
vowels("speeding")
>>> import re
>>> re.sub('([aeiou]+)','X\g<1>','speeding')
'spXeedXing'
>>>
You should create a new variable to track the previous letter in string. Check if the letters are continuous and only add X if the previous char is not the same as the current char.
def vowels(string):
newString = ""
i = 0 # Create a counter variable
for letter in string:
if (letter in "aeiou" and letter != string[i-1]) or (letter in "aeiou" and i == 0 and letter == string[i-1]): # Change this condition.
i += 1
newString += "X" + letter
else:
newString += letter
i += 1 # Increment counter variable
print(newString)
if __name__ == "__main__":
vowels("speeding")
Output:
spXeedXing
Other test cases:
vowels("oompaloompas")
XoompXalXoompXas
vowels("eerie")
XeerXiXe
You can let a regex do all the state-machine hard work...
To prepend 'X' to any number of consecutive vowels:
import re
s = 'speeding'
>>> re.sub(r'([aeiou]+)', r'X\1', s)
'spXeedXing'
To prepend 'X' only to the same repeating vowel:
s = 'speeding'
>>> re.sub(r'(([aeiou])\2*)', r'X\1', s)
'spXeedXing'
s = 'toaster'
>>> re.sub(r'(([aeiou])\2*)', r'X\1', s)
'tXoXastXer'
Here is a simple non-regex version:
def vowels(word):
new_word = ""
prev = "" # no previous letter at first
for letter in word:
if letter in "aeiou" and letter != prev:
new_word += "X" + letter
else:
new_word += letter
prev = letter # keep to avoid insertion for repeats
return new_word
if __name__ == "__main__":
print(vowels("speeding"))
print(vowels("eerie"))
print(vowels("aaaaaaaaaaaaaargh"))
producing
spXeedXing
XeerXiXe
Xaaaaaaaaaaaaaargh
you should try checking the previous letter in the string to see if it is the same letter as the current index
def vowels(string):
newString = ""
for i in range(len(string)):
if string[i] in "aeiou":
if string[i - 1] == string[i]:
newString += string[i]
else:
newString += "X" + string[i]
else:
newString += string[i]
print(newString)
I am the newbie in programming. The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.
But when the code meet ) -- closing parentheses it generate wrong output. As I figured out, the problem is with regular expressions, but I didn't get how to fix code.
from collections import Counter
def duplicate_encode(word):
counter = Counter(word.lower())
counter2 = dict.copy(counter)
print(counter2)
for k,v in counter2.items():
if counter2[k]==1:
counter2[k]='('
else:
counter2[k]=')'
for key in counter2.keys():
word = str(word.lower()).replace(key, str(counter2[key]))
return word
For example:
duplicate_encode('yy! R)!QvdG') should return )))((()(((( but I got (((((((((((.
from collections import Counter
def duplicate_encode(word):
res = list(word.lower())
counter = Counter(word.lower())
counter2 = dict.copy(counter)
print(counter2)
for k, value in enumerate(res):
if counter2[value] == 1:
res[k] = '('
else:
res[k] = ')'
# for key in counter2.keys():
# word = str(word.lower()).replace(key, str(counter2[key]))
return "".join(res)
res = duplicate_encode('yy! R)!QvdG')
print("res", res)
The problem arises when your input string contains a brace, like ( or ).
When that happens, like it does in your example, the wrong characters get replaced, which you can verify by adding print() statements in your code, every time you change word.
I've replaced that part in your code.
from collections import Counter
def duplicate_encode(word):
counter = Counter(word.lower())
counter2 = dict.copy(counter)
print(counter2)
for k,v in counter2.items():
if counter2[k]==1:
counter2[k]='('
else:
counter2[k]=')'
print(counter2)
new_word = ''
for character in word:
new_word += counter2[character.lower()]
return new_word
Note, however, that the code has quite a few redundancies and unnecessary usage of memory. It can be simplified to the following:
from collections import Counter
def duplicate_encode(word):
lower_word = word.lower()
new_word = ''
counter = Counter(lower_word)
for char in lower_word:
if counter[char] > 1:
new_word += ')'
else:
new_word += '('
return new_word
Another solution. Start with a set of (unique) characters, loop over characters in the string, removing the character from that set, and if it is already removed then it must be a duplicate. Use this to build up a set of duplicated characters.
def duplicate_encode(word):
word = word.upper()
s = set(word)
dups = set()
for char in word:
if char in s:
s.remove(char)
else:
dups.add(char)
return "".join(")" if char in dups else "("
for char in word)
print(duplicate_encode("'yy! R)!QvdG'"))
gives:
))))((()(((()
My task is to print all words in a sentence whose first letter is within a range of letters, for example: h-z.
This is my code so far, however it still prints words which begin with "g" and does not print the last word.
famous_quote = input("Enter a one sentence quote: ").lower()
word = ""
for ltr in famous_quote:
if ltr.isalpha() == True:
word = word + ltr
else:
if word > "g":
print(word)
word = ""
else:
word = ""
I'm only allowed to use ASCII comparisons, I've tried to compare the ASCII values but I don't know how to go about it in this context.
Sample input:
Wheresoever you go, go with all your heart
Sample output:
WHERESOEVER
YOU
WITH
YOUR
HEART
Algorithm I've come up with:
- split the words by building a placeholder variable: word
- Loop each character in the input string
- check if character is a letter
- add a letter to word each loop until a non-alpha char is encountered
- if character is alpha
- add character to word
- non-alpha detected (space, punctuation, digit,...) defines the end of a word and goes to else
- else
- check if word is greater than "g" alphabetically
- print word
- set word = empty string
- or else
- set word = empty string and build the next word
- Hint: use .lower()
You can define a neat little generator to split your sentence into words and compare the first letter of each.
def filter_words(sentence, lo, hi):
lo, hi = map(str.upper, (lo, hi))
words = sentence.upper().split()
for word in words:
if lo <= word[0] <= hi:
yield word
sentence = 'Wheresoever you go, go with all your heart'
print(*filter_words(sentence, 'h', 'z'), sep='\n')
WHERESOEVER
YOU
WITH
YOUR
HEART
This is how I approached this problem. It gave me a hard time since I am a beginer. But it seems to work fine.
quote = "quote goes here"
word = ""
for letter in quote:
if letter.isalpha():
word += letter
else:
if word > "":
print(word.upper())
word = ""
else:
word = ""
print(word.upper())
I added the space to the user_input and also used the word > 'h'. Below is how it looks:
user_input = input('Enter a phrase: ').lower()
user_input += ' '
word = ''
for char in user_input:
if char.isalpha():
word += char
else:
if word > 'h':
print(word.upper())
word = ''
else:
word = ''
This code worked for me...
The task is: Create a program inputs a phrase (like a famous quotation) and prints all of the words that start with h-z
I was making the mistake of using word > "g" before, which needs to be replaced by word > "h".
Also, you need to add the last print command in order to print the last word in case the phrase does not end with a punctuation (as in the given example)
phrase = input ("Please enter a phrase: ").lower()
word = ""
for letter in phrase:
if letter.isalpha():
word += letter
else:
if(word > "h" ):
print(word)
word = ""
else:
word = ""
if word.lower() > 'h':
print(word)
Just one comment on the exercise, as a programming exercise this approach is fine but you would never do it this way in practice.
The two issues you've highlighted is that you are comparing the whole word instead of just the first character.
Simply change:
if word > "g":
To:
if word and word[0] > "g":
And if the quote doesn't finish with a punctuation you will miss the last word off, just add after the loop:
if word:
print(word)
You may note the output is all uppercase, so .lower() the whole quotation may be an issue, alternatively you can just .lower() the comparison, e.g.:
famous_quote = input("Enter a one sentence quote: ")
...
if word and word[0].lower() > "g":
Note: You can simplify your else: condition:
else:
if word and word[0] > "g":
print(word)
word = ""
You stated that you are not allowed to use the split() method. I am not sure what you can use, so here's a solution (not the optimal one).
famous_quote = input("Enter a one sentence quote:") + ' '
current_word = None
for c in famous_quote:
if ('a' <= c <= 'z') or ('A' <= c <= 'Z'):
if current_word is None:
current_word = c # start a new word
else:
current_word += c # append a new letter to current word
else:
if current_word is not None:
f = current_word[0] # first letter
if ('h' <= f <= 'z') or ('H' <= f <= 'Z'):
print(current_word)
current_word = None
Here is a sample run of the program. It preserves lowercase and uppercase. It also splits words on any non-ASCII character.
Enter a one sentence quote: Whereever you go, there you are!!!
Whereever
you
there
you
Note: Since printing is done when a non-ASCII character is encountered, a non-ASCII character is appended at the end of famous_quote.
Assuming that the famous quote contains only spaces as word separator, this should do the job:
words = input("Enter a one sentence quote: ").lower().split()
for word in words:
if word[0] > 'g':
print("{w} ".format(w = word))
split() transforms a string into a list (array). It takes, by default, the space character as parameter (hence I did not give the argument) and returns the list of words.
print() can be used in a lot of ways, due to python's history with this function.
You can .join() the list (getting a string as result) and print it:
print(" ".join(words))
you can also print with concatenations (considered ugly):
print(word+" ")
or you can use formatted printing, which I do use a lot for readibility:
print("{w} ".format(w = word))
interprets "{w}" and replaces it with word wherever "{w}" appears.
Print formatting is rather CPU consuming (but it is still really fast). Usually any print operation slows your application, you want to minimize making outputs if you are making CPU intensive apps in your future (here I don't do that because CPU is not the main concern).
1. Split the words by building a placeholder variable: word
Loop each character in the input string
and check if character is a letter. Then add letter to the variable "word". Loop until a non-alpha char is encountered.
2. If character is alpha or (alphabet)
Add character to word.
Non-alpha detected (space, punctuation, digit,...) defines the end of a word and goes to the "else" part.
input_quote = input("Enter a 1 sentence quote, non - alpha seperate words: ")
word = ""
for character in input_quote:
if character.isalpha():
word += character
3. Else
Check if word is greater than "g" alphabetically. Print word and set "word = empty" string.
else:
if word and word[0].lower() >= "h":
print("\n", word.upper())
word = ""
4. Or else
Set word = empty string and build the next word.
else:
word = ""
if word.lower() >= "h":
print("\n", word.upper())
The last "if" is explicitly coded to print the last word if it doesn't end with a non-alpha character like a space or punctuation.
I did this exact same problem. The issue most people are having (and no one seemed to point out) is when you encounter double punctuations or a punctuation followed by a space.
This is the code I used.
phrase = input("Please enter a famous quote: ")
word = ""
for letter in phrase:
if letter.isalpha() is True:
word += letter
elif len(word) < 1: <--- [This is what accounts for double punctuations]
word = ""
elif word[0].lower() >= "g":
print(word)
word = ""
else:
word = ""
print(word) <--- [accounts for last word if not punctuated]
Variable "word" already contains your last word of the phrase but since it does not fulfil the condition to enter the loop it does not gets printed. So you can check the below solution.
phrase = input("Enter a phrase after this: ")
word = ""
for char in phrase:
if char.isalpha():
word += char
else:
if word != "":
if word[0].lower() >= "h":
print(word.upper())
word = ""
else:
word = ""
if word[0].lower() >= "h":
print(word.upper())
This code works for me:
phrase=input("Enter a one sentence quote,non-alpha separate words: ")
word=""
for character in phrase:
if character.isalpha():
word+=character
else:
if word.lower()>="h".lower():
print(word.upper())
word="" -----this code defines the end of a word
else:
word=""
print(word.upper()) ------this will print the last word
I would use regular expressions and list compreshension as shown in the function below.
def words_fromH2Z():
text = input('Enter a quote you love : ')
return [word for word in re.findall('\w+', text) if not word[0] in list('aAbBcCdDeEfFgG')]
When I test the function by putting in the input "I always Visit stack Overflow for Help", I get:
words_fromH2Z()
Enter a quote you love : I always Visit stack Overflow for Help
['I', 'Visit', 'stack', 'Overflow', 'Help']
This worked well for me. I had to add the last two lines of code because without them, it wasn't printing the last word, even if it began with a letter between h and z.
word = ""
quote = input("Enter your quote")
for char in quote:
if char.isalpha():
word += char
elif word[0:1].lower() > "g":
print(word.upper())
word = ""
else:
word = ""
if word[0:1].lower() > "g":
print(word.upper())
famous_quote = input("Enter a one sentence quote:")
current_word = None
for c in famous_quote:
if c.isalpha():
if (c >= 'a') or (c >= 'A'):
if current_word is None:
current_word = c
else:
current_word += c
else:
if current_word is not None:
f = current_word[0]
if ('h' <= f <= 'z') or ('H' <= f <= 'Z'):
print (current_word.upper())
current_word = None
if famous_quote[-1].isalpha():
print (current_word.upper())
Working through exercises on the CodeWars website and I need help being pointed in the right direction for a simple function:
Write a function toWeirdCase (weirdcase in Ruby) that accepts a
string, and returns the same string with all even indexed characters
in each word upper cased, and all odd indexed characters in each word
lower cased. The indexing just explained is zero based, so the
zero-ith index is even, therefore that character should be upper
cased.
The passed in string will only consist of alphabetical characters and
spaces(' '). Spaces will only be present if there are multiple words.
Words will be separated by a single space(' ').
The code I have so far is this:
def to_weird_case(string):
#TODO
new_string = ''
add = 0
for letter in range(len(string)):
if string[letter] == ' ':
add += 1
new_string += string[letter]
continue
if (letter+add)%2 == 0:
new_string += string[letter].upper()
else:
new_string += string[letter].lower()
print("Returning: " + new_string)
return new_string
I am trying to iterate over each letter while taking in to account the spaces but I am unsure how to 'skip over' the spaces and that is what is messing up my function? If someone could point me in the right direction that would be helpful, thanks.
def to_weird_case(string):
#TODO
counter = 0
new_string = ''
add = 0
for letter in range(len(string)):
if string[letter] == ' ':
new_string += string[letter]
continue
if (counter)%2 == 0:
new_string += string[letter].upper()
else:
new_string += string[letter].lower()
# Increment counter after one place as 0th position is even
counter = counter + 1
print("Returning: " + new_string)
return new_string
to_weird_case("HELLO MY NAME IS abcdefghijk")
Output : Returning: HeLlO mY nAmE iS aBcDeFgHiJk
Just create a counter (an integer variable) that will keep track of whether you are in an even or odd index. The counter will not increment if you encounter a space, thereby ignoring it.
def to_weird_case(string):
#TODO
counter = 0
new_string = ''
add = 0
for letter in range(len(string)):
if string[letter] == ' ':
new_string += string[letter]
continue
# Increment counter only if not space
counter = counter + 1
if (counter)%2 == 0:
new_string += string[letter].upper()
else:
new_string += string[letter].lower()
print("Returning: " + new_string)
return new_string
You can disregard the spaces by first splitting the string using str.split, transform each word using enumerate to select even and odd characters, and then rejoin the string using str.join on spaces:
def transform_word(s):
return ''.join(x.upper() if i%2==0 else x.lower() for i, x in enumerate(s))
# ^------------^-> ternary operator for alternating
def to_weird_case(string):
return ' '.join(transform_word(s) for s in string.split())
print(to_weird_case('This is a lovely day'))
# ThIs Is A LoVeLy DaY
And if you eventually want to consider the spaces, use the transform_word function directly:
print(transform_word('This is a lovely day'))
# ThIs iS A LoVeLy dAy
Try list comprehension:
def my_func(your_string):
x = [ x.upper() if i%2==0 else x.lower() for i,x in enumerate(your_string)]
return ''.join(x)
your_string = 'hello my name is lmurdock12'
print(my_func(your_string))
Output:
HeLlO My nAmE Is lMuRdOcK12
So basically what happens in this list comprehension is that we use enumerate()
x = [ x.upper() if i%2==0 else x.lower() for i,x in enumerate(your_string)]
enumerate takes a iterable (string,list..etc) and gives out items one by one (i) where i would be 0,1,2,3...and so on
So in the list comprehension we check if i corresponding to that item x returned from iterable string your_list. So in hello my name is lmurdock12 i would be 0 for h and 1 for e and so on. If i%2==0 which means it's even we keep x.upper() else we keep x.lower() in the list.
Finally use ''.join(x) to join the list and return it if you want.
NOTE: This could be done without enumerate by using index() but that would make our algorithm a little inefficient because that we way we have search for the value and return index everytime. It's better to use enumerate.
Here is a working solution. Hope that helps!
def to_weird_case(string)
words = string.split(" ")
words.map do |word|
word.chars.map.with_index {|letter, idx| idx.even? ? letter.upcase : letter.downcase }.join("")
end.join(" ")
end
You can avoid the counter if you user the enumerate function
def to_weird_case(string):
#TODO
new_string = ''
for i,letter in enumerate(string):
if string[i] == ' ':
new_string += string[i]
continue
if i%2 == 0:
new_string += string[i].upper()
else:
new_string += string[i].lower()
print("Returning: " + new_string)
return new_string
Your code is not really pythonesque. Your function can actually have only 1 line of code. This is why Python is so cool.
def ToWeirdCase(word):
return ''.join([char.upper() if pos%2==0 else char.lower() for pos, char in enumerate(word)])
Hi I had this same problem early this morning, this is my answer:
def to_weird_case(sentence):
counter = 0 # Counter set to zero, because of the even number fact at zero-ith.
weird_sentence = "" # This is the string we are going to return at the end.
for character in sentence:
if character == " " and counter % 2 == 0:
weird_sentence += character
counter += 1 # Incrementation that will now make each characters before a space to be uppercased.
elif character != " " and counter % 2 == 0:
weird_sentence += character.upper()
else:
weird_sentence += character.lower()
counter += 1
return weird_sentence
I hope it helps you out.
Input : UserID/ContactNumber
Output: user-id/contact-number
I have tried the following code:
s ="UserID/ContactNumber"
list = [x for x in s]
for char in list:
if char != list[0] and char.isupper():
list[list.index(char)] = '-' + char
fin_list=''.join((list))
print(fin_list.lower())
but the output i got is:
user-i-d/-contact-number
You could use a regular expression with a positive lookbehind assertion:
>>> import re
>>> s ="UserID/ContactNumber"
>>> re.sub('(?<=[a-z])([A-Z])', r'-\1', s).lower()
'user-id/contact-number'
What about something like that:
s ="UserID/ContactNumber"
so = ''
for counter, char in enumerate(s):
if counter == 0:
so = char
elif char.isupper() and not (s[counter - 1].isupper() or s[counter - 1] == "/"):
so += '-' + char
else:
so += char
print(so.lower())
What did I change from your snippet?
You were checking if this is the first char and if it is a upper char.
I have add a check on the previous char, to not consider when the previous is upper (for the D of ID) or when the previous is \ for the C of Contact.
You are also editing the list list while iterating on it's element, that is dangerous.
I have instead create an output string to store the new values
I did something like this
s ="UserID/ContactNumber"
new = []
words = s.split("/")
for word in words:
new_word = ""
prev = None
for i in range(0, len(word)):
if prev is not None and word[i].isupper() and word[i-1].islower():
new_word = new_word + "-" + word[i]
prev = word[i]
continue
new_word = new_word + word[i]
prev = word[i]
new.append(new_word.lower())
print "/".join(new)
user-id/contact-number