I'm trying to reformat a series of strings into Camel Case by returning the fragments from input as a single "sentence".
this is what I have so far -
def convert(s):
if(len(s) == 0):
return
s1 = ''
s1 += s[0].upper()
for i in range(1, len(s)):
if (s[i] == ' '):
s1 += s[i + 1].upper()
i += 1
elif(s[i - 1] != ' '):
s1 += s[i]
print(s1)
# Driver Code
def main():
s = "BusOneBusTWOBUSthree"
convert(s)
if __name__=="__main__":
main()
output I'm getting is -
Bus One
Bus TWO
BUS three
output I'm wanting to get is -
busOne busTwo busThree
It looks like your example s= won't get you the output you say you're getting, so I am guessing that your input is space separated. It looks like you want to alternatively join 2 words together and make the 2nd word's first letter Uppercase. If so, split the string into a list, then iterate through the list and add to your final string. Here's one way to do that:
original_string = 'buS one buS two bus thRee'
original_string = original_string.lower()
list_of_words = original_string.split()
# splits on space by default, or specify 'x' to split on the letter x
camel_case_output = ''
for i in range(len(list_of_words)):
this_word = list_of_words[i]
if i % 2 == 0: # even numbers
camel_case_output += list_of_words[i]
else:
this_word = this_word[0].upper() + this_word[1:]
camel_case_output += this_word + ' '
camel_case_output.strip() # remove the last space, if you ended with extra
Related
I am looking for a way to add spaces between words in a string until len(string) < n is reached.
I've tried this:
string = "you are great"
n = 20
res = []
for i in string:
if ord(i) == 32 and len(string) < num:
res = [string + i]
And I want it to add spaces between "you" and "are", and "are" and "great.
So it gives me something like this:
res = ["you are great"]
But instead I get this
"you are great "
No need of loops:
string = "you are great"
n = 20
a = n - len(string.replace(' ', '')) # amount of spaces to add
b = string.split() # list of words (without spaces)
if a & 1:
b[0] += ' ' # if 'a' is odd, add an extra space to first word
d = (' ' * (a // 2)).join(b) # concatenate all words with 'a' spaces between them
print(d)
The output is:
"you are great"
You code is doing what it is suppose to do but as you can see in the second output it adds a space to your string. The code below is the issue.
Res = [string + i]
I wrote a function which takes a string and gives back the count of small letters and the count of capital letters in that string. The program works for single word but as soon I add two words containing 'space' in between two words, messes things up. spaces counts too.
What is your thoughts?
def myfunc(s):
s = str(s)
upperl = 0
lowerl = 0
for i in s:
if i == i.lower():
lowerl += 1
if i == i.upper():
upperl += 1
if i == ' ':
continue
return upperl,lowerl
x = myfunc('hello G')
print (x)
from the word 'hello G' we expect upper letter and lower letter
count as 1,5 but that space between two words makes it 2,6.
The problem is that ' ' == ' '.upper() and ' ' == ' '.lower() are both true, and you're not checking whether you're currently dealing with an alphanumeric character or something else. Instead, you can check whether you're working with a lowercase letter or an uppercase letter.
Try this:
def calculate_case_count(string: str):
string = str(string)
upper_letter_count = 0
lower_letter_count = 0
for letter in string:
if letter.islower():
lower_letter_count += 1
elif letter.isupper():
upper_letter_count += 1
return upper_letter_count, lower_letter_count
result = calculate_case_count('hello G ')
print(result) # (1, 5)
Using regex will be cleaner solution here
import re
def count_letter_cases(text):
n_lower = len(re.findall("[a-z]", text))
n_upper = len(re.findall("[A-Z]", text))
return n_lower, n_upper
print(count_letter_cases("Hello Goat"))
## Result: (7,2)
from collections import Counter
def count_cases(strng):
counts = Counter(strng)
upper = 0
lower = 0
for char, count in counts.items():
if char.isupper():
upper += count
elif char.islower():
lower += count
return (upper, lower)
Edit: Removed string module. Using internal islower and isupper methods.
I have a text file with a huge text written in paragraphs.
I need to count certain punctuation symbols:
without using any module, not even regex
count , and ;
also needs to count ' and -, but only under certain circumstances. Specifically:
count ' marks, but only when they appear as apostrophes surrounded by letters, i.e. indicating a contraction such as "shouldn't" or "won't". (Apostrophe is being included as an indication of more informal writing, perhaps direct speech.)
count - signs, but only when they are surrounded by letters, indicating a compound-word, such as "self-esteem".
Any other punctuation or letters, e.g. digits, should be regarded as white space, so serve to end words.
Note: Some of the texts we will use include double hyphen, i.e. --. This is to be regarded as a space character.
I first created a string and stored some punctuations inside it for example punctuation_string = ";./'-" but it is giving me the total; what I need is count for individual punctuation.
Because of that I have to change certain_cha variable number of times.
with open("/Users/abhishekabhishek/downloads/l.txt") as f:
text_lis = f.read().split()
punctuation_count = {}
certain_cha = "/"
freq_coun = 0
for word in text_lis:
for char in word:
if char in certain_char:
freq_coun += 1
punctuation_count[certain_char] = freq_count
I need values to be displayed like this:
; 40
. 10
/ 5
' 16
etc.
but what I get is total (71).
You will need to create a dictionary where each entry stores the count of each of those punctuation characters.
For commas and semicolons, we can simply do a string search to count the number of occurences in a word. But we'll need to handle ' and - slightly differently.
This should take care of all the cases:
with open("/Users/abhishekabhishek/downloads/l.txt") as f:
text_words = f.read().split()
punctuation_count = {}
punctuation_count[','] = 0
punctuation_count[';'] = 0
punctuation_count["'"] = 0
punctuation_count['-'] = 0
def search_for_single_quotes(word):
single_quote = "'"
search_char_index = word.find(single_quote)
search_char_count = word.count(single_quote)
if search_char_index == -1 and search_char_count != 1:
return
index_before = search_char_index - 1
index_after = search_char_index + 1
# Check if the characters before and after the quote are alphabets,
# and the alphabet after the quote is the last character of the word.
# Will detect `won't`, `shouldn't`, but not `ab'cd`, `y'ess`
if index_before >= 0 and word[index_before].isalpha() and \
index_after == len(word) - 1 and word[index_after].isalpha():
punctuation_count[single_quote] += 1
def search_for_hyphens(word):
hyphen = "-"
search_char_index = word.find(hyphen)
if search_char_index == -1:
return
index_before = search_char_index - 1
index_after = search_char_index + 1
# Check if the character before and after hyphen is an alphabet.
# You can also change it check for characters as well as numbers
# depending on your use case.
if index_before >= 0 and word[index_before].isalpha() and \
index_after < len(word) and word[index_after].isalpha():
punctuation_count[hyphen] += 1
for word in text_words:
for search_char in [',', ';']:
search_char_count = word.count(search_char)
punctuation_count[search_char] += search_char_count
search_for_single_quotes(word)
search_for_hyphens(word)
print(punctuation_count)
following should work:
text = open("/Users/abhishekabhishek/downloads/l.txt").read()
text = text.replace("--", " ")
for symbol in "-'":
text = text.replace(symbol + " ", "")
text = text.replace(" " + symbol, "")
for symbol in ".,/'-":
print (symbol, text.count(symbol))
Because you don't want to import anything this will be slow and will take some time, but it should work:
file = open() # enter your file path as parameter
lines = file.readline() # enter the number of lines in your document as parameter
search_chars = [',', ';', "'", '-'] # store the values to be searched
search_values = {',':0, ';':0, "'":0, '-':0} # a dictionary saves the number of occurences
whitespaces = [' ', '--', '1', '2', ...] # you can add to this list whatever you need
for line in lines:
for search in search_chars:
if search in line and (search in search_chars):
chars = line.split()
for ch_index in chars:
if chars [ch_index] == ',':
search_values [','] += 1
elif chars [ch_index] == ';':
search_values [';'] += 1
elif chars[ch_index] == "'" and not(chars[ch_index-1] in whitespaces) and not(chars[ch_index+1] in whitespaces):
search_values ["'"] += 1
elif chars[ch_index] == "-" and not(chars[ch_index-1] in whitespaces) and not(chars[ch_index+1] in whitespaces):
search_values ["-"] += 1
for key in range(search_values.keys()):
print(str(key) + ': ' + search_values[key])
This is obviously not optimal and it is better to use regex here, but it should work.
Feel free to ask if any questions should arise.
Ex. Input: rat the ate cat the
Output: the cat ate the rat
Here's my code so far:
def reverse_message(starting, ending, msg):
while(starting < ending):
msg[starting], msg[ending] = msg[ending], msg[starting]
starting += 1
ending -= 1
def reverse_words(msg):
# Decode the message by reversing the words
# reverse entire message
reverse_message(0, len(msg) - 1, msg)
#reverse each word
starting = 0
for i in range(len(msg)):
if ((msg[i] == ' ') or (i == len(msg) - 1)):
reverse_message(starting, i-1, msg)
starting = i+1
What am I doing wrong? Any help would be highly appreciated.
This can be done in a single line:
str=' '.join(list(input().split(' '))[::-1])
To begin with, I would avoid explicitly passing a starting and ending index, instead relying on the message itself, where starting index is the first, and the ending index is the last index of the string, also I will pass the string as a list, since strings are mutable and cannot be changed, but list can.
def reverse_word(msg):
starting = 0
ending = len(msg)-1
while(starting < ending):
tmp = msg[starting]
msg[starting] = msg[ending]
msg[ending] = tmp
starting += 1
ending -= 1
return msg
After that, to reverse the string, I will reverse the entire string first, and then reverse each word in the string in place, and then stitch the string back together for the output.
def reverse_message(msg):
#Convert the string into list of characters
chars = list(msg)
#Reverse entire list
chars = reverse_word(chars)
starting = 0
i = 0
result = []
#Iterate through the reversed list, and pick individual words based on
#whitespace, and then reverse them in place
while i < len(chars):
if chars[i] == ' ':
#Append all reversed words to another list
result += reverse_word(chars[starting:i]) + [' ']
starting = i+1
i+=1
#Reverse the last remaining word
result += reverse_word(chars[starting:i])
#Stitch the list back to string and return it
return ''.join(result)
The resultant output will look like.
print(reverse_message('rat the ate cat the'))
#the cat ate the rat
I have a string with multiple words separated by underscores like this:
string = 'this_is_my_string'
And let's for example take string[n] which will return a letter.
Now for this index I want to get the whole word between the underscores.
So for string[12] I'd want to get back the word 'string' and for string[1] I'd get back 'this'
Very simple approach using string slicing is to:
slice the list in two parts based on position
split() each part based on _.
concatenate last item from part 1 and first item from part 2
Sample code:
>>> my_string = 'this_is_my_sample_string'
# ^ index 14
>>> pos = 14
>>> my_string[:pos].split('_')[-1] + my_string[pos:].split('_')[0]
'sample'
This shuld work:
string = 'this_is_my_string'
words = string.split('_')
idx = 0
indexes = {}
for word in words:
for i in range(len(word)):
idx += 1
indexes[idx] = word
print(indexes[1]) # this
print(indexes[12]) #string
The following code works. You can change the index and string variables and adapt to new strings. You can also define a new function with the code to generalize it.
string = 'this_is_my_string'
sp = string.split('_')
index = 12
total_len = 0
for word in sp:
total_len += (len(word) + 1) #The '+1' accounts for the underscore
if index < total_len:
result = word
break
print result
A little bit of regular expression magic does the job:
import re
def wordAtIndex(text, pos):
p = re.compile(r'(_|$)')
beg = 0
for m in p.finditer(text):
#(end, sym) = (m.start(), m.group())
#print (end, sym)
end = m.start()
if pos < end: # 'pos' is within current split piece
break
beg = end+1 # advance to next split piece
if pos == beg-1: # handle case where 'pos' is index of split character
return ""
else:
return text[beg:end]
text = 'this_is_my_string'
for i in range(0, len(text)+1):
print ("Text["+str(i)+"]: ", wordAtIndex(text, i))
It splits the input string at '_' characters or at end-of-string, and then iteratively compares the given position index with the actual split position.