What this 1 stands for in following code? - python

def up_low(s):
u = sum(1 for i in s if i.isupper())
l = sum(1 for i in s if i.islower())
print(s)
print( "No. of Upper case characters : %s, \nNo. of Lower case characters : %s" % (u,l))

Lets look at one of the lines and explain each path of it.
u = sum(1 for i in s if i.isupper())
sum() - A function that sums the values in a given list
inside the sum there is a generator expression (PEP-289).
The generator expression is:
1 for i in s if i.isupper()
what it actually does is:
for each value in s: # which means, iterate over the characters
if value is uppercase character:
add an integer with the value 1 to the list
which means - count the number of uppercase characters in the given string.
Lets use it in an example:
s = "HeLlo WoRLD" # We have 6 uppercase characters here.
upper_case_count_list = [1 for i in s if i.isupper()] # Evaluates to: [1,1,1,1,1,1] - A 1 for every uppercase character in s.
sum(upper_case_count_list) # Sums the numbers in our list, which will sum up to 6

Related

finding the minimum window substring

the problem says to create a string, take 3 non-consecutive characters from the string and put it into a sub-string and print the which character the first one is and which character the last one is.
str="subliminal"
sub="bmn"
n = len(str)-3
for i in range(0, n):
print(str1[i:i+4])
if sub1 in str1:
print(sub1[i])
this should print 3 to 8 because b is the third letter and n is the 8th letter.
i also don't know how to make the code work for substrings that aren't 3 characters long without changing the code in total.
Not sure if this is what you meant. I assume that the substring is already valid, which means that it contains non consecutive letters. Then I get the first and last letter of the substring and create a list of all the letters in the string using a list comprehension. Then i just loop through the letters and save where the first and last letter occur. If anything is missing, hmu.
sub = "bmn"
str = "subliminal"
first_letter = sub[0]
last_letter = sub[-1]
start = None
end = None
letters = [let for let in str]
for i, letter in enumerate(letters):
if letter == first_letter:
start = i
if letter == last_letter:
end = i
if start and end:
print(f"From %s to %s." % (start + 1, end + 1)) # Output: From 3 to 8.
Some recursion for good health:
def minimum_window_substring(strn, sub, beg=0, fin=0, firstFound=False):
if len(sub) == 0 or len(strn) == 0:
return f'From {beg + 1} to {fin}'
elif strn[0] == sub[0]:
return minimum_window_substring(strn[1:], sub[1:], beg, fin + 1, True)
if not firstFound:
beg += 1
return minimum_window_substring(strn[1:], sub, beg, fin + 1, firstFound)
Explanation:
The base case is if we get our original string or our sub-string to be length 0, we then stop and print the beginning and the end of the substring in the original string.
If the first letter of the current string is equal then we start the counter (we fix the beginning "beg" with the flag "firstFound") Then increment until we finish (sub is an empty string / original string is empty)
Something to think about / More explanation:
If for example, you ask for the first occurrence of the substring, for example if the original string would be "sububusubulum" and the sub would equal to "sbl" then when we hit our first "s" - it means it would 100% start from there, because if another "sbl" is inside the original string - then it must contain the remaining letters, and so we would say they belong to the first s. (A horrible explanation, I am sorry) what I am trying to say is that if we have 2 occurrences of the substring - then we would pick the first one, no matter what.
Note: This function does not really care if the sub-string contains consecutive letters, also, it does not check whether the characters are in the string itself, because you said that we must be given characters from the original string. The positive thing about it, is that the function can be given more than (or less than) 3 characters long substring
When I say "original string" I mean subliminal (or other inputs)
There are many different ways you could do it,
here is a soultion,
import re
def Func(String, SubString):
patt = "".join([char + "[A-Za-z]" + "+" for char in sub[:-1]] + [sub[-1]])
MatchedString = re.findall(patt, String)[0]
FirstIndex = String.find(MatchedString) + 1
LastIndex = FirstIndex + len(MatchedString) -1
return FirstIndex, LastIndex
string="subliminal"
sub="bmn"
FirstIndex, LastIndex = Func(string, sub)
This will return 3, 8 and you could change the length of the substring, and assuming you want just the first match only

How to efficiently remove single letter words in Python

I want to remove single letter words such as a, i e, e g, b f f, y o l o, c y l using a Python function.
My current code looks follows.
def remove_single_letters(concept):
mystring = "valid"
if len(concept) == 1:
mystring = "invalid"
if len(concept)>1:
validation = []
splits = concept.split()
for item in splits:
if len(item) > 1:
validation.append("valid")
if len(validation) != 1:
mystring = "invalid"
return mystring
print(remove_single_letters("b f f"))
It works fine. However, I am wondering if there is a more efficient way (with lesser time) of doing it in python.
Here is a single line solution:
def remove_single_letters(concept):
return ["valid", "invalid"][concept.count(" ") >= len(concept) // 2]
Update: Note that this is shorter and cool looking but does not necessarily run faster.
Explanation:
concept.count(" "): Returns the number of spaces in string
>= len(concept) // 2: Returns True if more than half of the string is spaces (it fails when there are multiple spaces between legitimate words as #user202729 mentioned)
["valid", "invalid"][result]: This part is just for fun: It returns the first element if the result is False and the second element if the result is True (because False is equal to 0 and True is equal to 1).
I would go for a more concise solution (yet not faster as both solutions are of O(n)) if you want to check if any 1 letter character exists in the string:
remove_single_letters = lambda concept:"invalid" if 1 in [len(item) for item in concept.split()] else "valid"
print(remove_single_letters("a b c"))
#prints invalid
An ordinary function would be like this:
def remove_single_letters(concept):
return "invalid" if 1 in [len(item) for item in concept.split()] else "valid"
They both check the length of elements in the split input to find any item with length of 1 and is insensitive to multiple spaces thanks to split().
If you want to check the strings that are totally made up of single characters:
def remove_single_letters(concept):
u = set(len(item) for item in concept.split())
return "invalid" if len(u) == 1 and 1 in u else "valid"

How to define a function that counts the lower cases letters until reaching a value?

Define a function countLowerFromUntil(...) which receives one string (st) and an integer value (start) as input. The string may potentially also be the empty string. This function should return how many lower case letters there are in the input string st, starting to count (and including) the start position and advancing one position at a time until reaching the end of the string or until reaching a digit (if there is such). The string may contain letters or digits. If the start value is out of the string range the function should return 0. Note: Keep in mind the string method islower() which returns true is applied to a character or a string containing only lower case letters.
For example countLowerFromUntil("ABCxAxx1aa") should return 3, because there are three lower case letters (3 "x"'s) before reaching the digit 1
As an example, the following code fragment:
val = countLowerFromUntil("ABCxAxx1aa",0)
print (val)
should produce the output:
3
so far I have this but I get an error:
def countLowerFromUntil(st,ch):
s = st().strip()
count = 1
for i in s:
if i.islower():
count = count + 1
return count
You need to take into account start. Start iterating from the start index. You can slice what you need. You don't need to strip (stripping spaces might invalidate start)
The algorithm goes like this:
counts begin from 0
if you encounter a digit, stop counting!
if you encounter a lower-case character, increment your counter.
return count at the end of the function
def f(string, start):
count = 0
for c in string[start:]:
if c.isdigit():
break
elif c.islower():
count += 1
return count
>>> f("ABCxAxx1aa", 0)
3

Most common character in a string

Write a function that takes a string consisting of alphabetic
characters as input argument and returns the most common character.
Ignore white spaces i.e. Do not count any white space as a character.
Note that capitalization does not matter here i.e. that a lower case
character is equal to a upper case character. In case of a tie between
certain characters return the last character that has the most count
This is the updated code
def most_common_character (input_str):
input_str = input_str.lower()
new_string = "".join(input_str.split())
print(new_string)
length = len(new_string)
print(length)
count = 1
j = 0
higher_count = 0
return_character = ""
for i in range(0, len(new_string)):
character = new_string[i]
while (length - 1):
if (character == new_string[j + 1]):
count += 1
j += 1
length -= 1
if (higher_count < count):
higher_count = count
return (character)
#Main Program
input_str = input("Enter a string: ")
result = most_common_character(input_str)
print(result)
The above is my code. I am getting an error of string index out of bound which I can't understand why. Also the code only checks the occurrence of first character I am confused about how to proceed to the next character and take the maximum count?
The error i get when I run my code:
> Your answer is NOT CORRECT Your code was tested with different inputs.
> For example when your function is called as shown below:
>
> most_common_character ('The cosmos is infinite')
>
> ############# Your function returns ############# e The returned variable type is: type 'str'
>
> ######### Correct return value should be ######## i The returned variable type is: type 'str'
>
> ####### Output of student print statements ###### thecosmosisinfinite 19
You can use a regex patter to search for all characters. \w matches any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9_]. The + after [\w] means to match one or more repetitions.
Finally, you use Counter to total them and most_common(1) to get the top value. See below for the case of a tie.
from collections import Counter
import re
s = "Write a function that takes a string consisting of alphabetic characters as input argument and returns the most common character. Ignore white spaces i.e. Do not count any white space as a character. Note that capitalization does not matter here i.e. that a lower case character is equal to a upper case character. In case of a tie between certain characters return the last character that has the most count"
>>> Counter(c.lower() for c in re.findall(r"\w", s)).most_common(1)
[('t', 46)]
In the case of a tie, it is a little more tricky.
def top_character(some_string):
joined_characters = [c for c in re.findall(r"\w+", some_string.lower())]
d = Counter(joined_characters)
top_characters = [c for c, n in d.most_common() if n == max(d.values())]
if len(top_characters) == 1:
return top_characters[0]
reversed_characters = joined_characters[::-1]
for c in reversed_characters:
if c in top_characters:
return c
>>> top_character(s)
't'
>>> top_character('the the')
'e'
In the case of your code above and your sentence "The cosmos is infinite", you can see that 'i' occurs more frequently that 'e' (the output of your function):
>>> Counter(c.lower() for c in "".join(re.findall(r"[\w]+", 'The cosmos is infinite'))).most_common(3)
[('i', 4), ('s', 3), ('e', 2)]
You can see the issue in your code block:
for i in range(0, len(new_string)):
character = new_string[i]
...
return (character)
You are iterating through a sentence and assign that letter to the variable character, which is never reassigned elsewhere. The variable character will thus always return the last character in your string.
Actually your code is almost correct. You need to move count, j, length inside of your for i in range(0, len(new_string)) because you need to start over on each iteration and also if count is greater than higher_count, you need to save that charater as return_character and return it instead of character which is always last char of your string because of character = new_string[i].
I don't see why have you used j+1 and while length-1. After correcting them, it now covers tie situations aswell.
def most_common_character (input_str):
input_str = input_str.lower()
new_string = "".join(input_str.split())
higher_count = 0
return_character = ""
for i in range(0, len(new_string)):
count = 0
length = len(new_string)
j = 0
character = new_string[i]
while length > 0:
if (character == new_string[j]):
count += 1
j += 1
length -= 1
if (higher_count <= count):
higher_count = count
return_character = character
return (return_character)
If we ignore the "tie" requirement; collections.Counter() works:
from collections import Counter
from itertools import chain
def most_common_character(input_str):
return Counter(chain(*input_str.casefold().split())).most_common(1)[0][0]
Example:
>>> most_common_character('The cosmos is infinite')
'i'
>>> most_common_character('ab' * 3)
'a'
To return the last character that has the most count, we could use collections.OrderedDict:
from collections import Counter, OrderedDict
from itertools import chain
from operator import itemgetter
class OrderedCounter(Counter, OrderedDict):
pass
def most_common_character(input_str):
counter = OrderedCounter(chain(*input_str.casefold().split()))
return max(reversed(counter.items()), key=itemgetter(1))[0]
Example:
>>> most_common_character('ab' * 3)
'b'
Note: this solution assumes that max() returns the first character that has the most count (and therefore there is a reversed() call, to get the last one) and all characters are single Unicode codepoints. In general, you might want to use \X regular expression (supported by regex module), to extract "user-perceived characters" (eXtended grapheme cluster) from the Unicode string.

Alphabet to integers

I'm trying to create a programm in which a user inputs a string e.g 'roller' and the program converts the alphabet to numbers such as a=1, b=2, c=3 etc, and the calculate the sum of these values. But, if the program finds two same letters in a row then it doubles the sum. So far I have done this:
input = raw_input('Write Text: ')
input = input.lower()
output = []
sum=0
for character in input:
number = ord(character) - 96
sum=sum+number
output.append(number)
print sum
which calculates the sum of the characters and also appends the converted characters to a new array. So can anyone help me to double the sum if two letters appear in a row?
Store the previous character and compare it to the current character. If they're the same, double the value.
word = 'hello'
out = []
c_prev = None
for c in word:
value = ord(c) - ord('a')
if c == c_prev: # double if repeated character
value = value * 2
out.append(value)
c_prev = c # store for next comparison
print(sum(out))

Categories

Resources