This question already has answers here:
Python: A program to find the LENGTH of the longest run in a given list?
(6 answers)
Closed 4 years ago.
I'm trying to figure out how to check if certain characters are repeated after each other in a single string, and if so, how often are they repeated.
Example:
str.x = 'abbbjjaaaal'
As the return I need the integer 4, as in this case the longest consecutive repetition of a single character in the string x is a, and it is repeated 4 times.
some_str = 'abbbjjaaaal'
groups = [(k , len(list(g))) for k, g in groupby(a, str)]
groups.sort(key=lambda k:k[1], reverse=True)
print(grups[0][0])
Related
This question already has answers here:
How are strings compared?
(7 answers)
Closed 2 years ago.
How does min() sort a list like this: ["abc", "abb", "aba"], in the end I know the output of min(["abc", "abb", "aba"]) will be "aba" but I'm not quite sure how it decides on which one to consider the min, does it sort by the string with the lowest sum of ord() for each character in the string? Or how does it sort it?
It loops through each and every value and picks up the smallest one. Equivalent code:
def min(lst):
me = lst[0]
for i in range(1, len(lst)):
if lst[i] < me:
me = lst[i]
return me
It's a naive implementation but it gives you an idea.
This question already has answers here:
Code to output the first repeated character in given string?
(10 answers)
Closed 2 years ago.
You are given a string S
Your task is to find the first occurrence of an alphanumeric character in S(read from left to right) that has consecutive repetitions.
Input Format
A single line of input containing the string S.
Output Format
Print the first occurrence of the repeating character. If there are no repeating characters, print -1.
For example,
If I input the below string
..12345678910111213141516171820212223
the result is
1
I solved this problem but that's not correct.
My code is
def firstRepeatedChar(str):
h = {}
for ch in str:
if ch in h:
return ch;
else:
h[ch] = 0
return -1
n = input()
print(firstRepeatedChar(n))
but if I input the ^^cmmit^^, the result is not correct.
How can I solve this problem with python? Please help me.
A pythonic way to do something like this is to use zip() to make consecutive pairs of tuples (like (1, 2), (2, 3) ... and then just return the first one where both are equal and alphanumeric.
next() takes an optional parameter for what to return when there's nothing left, to which here you can pass -1. isalnum() can be used to test is a string is alphanumeric:
def firstRepeatedChar(s):
return next((j for j, i in zip(s, s[1:]) if j == i and j.isalnum()), -1)
firstRepeatedChar("..12345678910111213141516171820212223")
# 1
firstRepeatedChar("^^cmmit^^")
# 'm'
firstRepeatedChar("^^cmit^^")
# -1
This question already has answers here:
confusion with encryption
(3 answers)
Closed 3 years ago.
I need to find the best way to assign each letter of the alphabet a random number 1-26 (using each number once). Or assign each number 1-26 a random letter of the alphabet (using each letter once).
For example:
a = 6, b = 12, c = 91
or
1 = g, 2 = a, 3 = k
I've tried assigning randint to each letter and repeating if the number has been used already but its really long.
Why not something like this?
import random
x=['A','B','C'.....]
y=list(range(1,27))
random.shuffle(y)
combo=list(zip(x,y))
you could of course make the final output a dict or something
This question already has answers here:
Best way to determine if a sequence is in another sequence?
(10 answers)
Counting sublists in a list
(2 answers)
Closed 4 years ago.
I am doing homework and I got stuck with my code.
The question is about two lists containing string letters in it as elements and we want to know how many times the small text occurs in the long one.(with order)
Example:
long=['a','k','g','j','a','k','k','a','k','g']
small=['a','k','g']
then the answer should be 2 as it occurs twice(first and in the last part)
As I tried:
def search(long,small):
words=[]
for k in range(len(long)):
for l in range(len(small)):
if long[k]==small[l]:
words.append()
return(words)
First tried to write down the matches in the list and then divide the len of list to get the number. But unfortunatly this gives me the first match and i am not sure how to get all the matches. Hope you guys can help me.
You can do it by advancing through elements of small and resetting the counter if you encounter an "odd" element:
def search(long,small):
indexCt = 0
count = 0
for k in range(len(long)):
if long[k]==small[indexCt]:
indexCt += 1
else:
indexCt = 0
if(indexCt == len(small)):
indexCt = 0
count += 1
return count
Slice the long list into pieces len(small) long and check for equality each time
def search(long, small):
return sum(long[i:i+len(small)]==small for i in range(len(long)-len(small)+1))
print(search(['a','k','g','j','a','k','k','a','k','g'], ['a','k','g']))
Output:
2
This question already has answers here:
Generate a random letter in Python
(23 answers)
Generate 'n' unique random numbers within a range [duplicate]
(4 answers)
Closed 4 years ago.
I am trying to write a function that generates a random sequence of letters (letters from a list), and the sequence length is K (K is passed to the function as an argument when function is called). I am stuck, and I don't understand how to generate random sequence of letters.
As mentioned by we can loop the random choice on the given list for k times to get the desired output
Def fn(letters,k):
return [np.random.choice(letters) for _ range(k) ]]