I'm practicing loops. I've been trying to figure out how to get each character printed 4 times and place aside the others, eg: aaaaccccddddcccc. Right now the returned res is only showing me the last character printed 4 times (cccc). I tried nested for loops but that wasn't working for me either. How do I concatenate the returns? (or is there another way)
def quad_char(letter):
res = ''
for i in letter:
res = i * 4
return res
print(quad_char('acdc'))
You were almost there! :-)
On every loop, you are re-assigning to res, instead of appending to it (you overwrite res on every iteration, basically).
Try:
def quad_char(letter):
res = ''
for i in letter:
res = res + (i * 4)
return res
print(quad_char('acdc'))
Which can be shortened to:
def quad_char(letter):
res = ''
for i in letter:
res += i * 4
return res
print(quad_char('acdc'))
More on string concatenation here.
Also worth looking into the performance of various ways to concatenate strings here.
change res = i * 4 to res += i * 4
Could also do:
def quad_char(letter):
for i in letter:
print ("".join(i*4), end="")
forget the explicit for loop and use a list comprehension with a join
def quad_char(letters):
return ''.join(l*4 for l in letters)
Related
I am a newbie in python and I am working on a function that I expect to pass a string like abcd and it outputs something like A-Bb-Ccc-Dddd.
I have created the following.
`
def mumbler(s):
chars = list(s)
mumbled = []
result = []
for char in chars:
caps = char.upper()
num = chars.index(char)
low = char.lower()
mumbled.append( caps+ low*num)
for i in mumbled:
result.append(i+'-')
result = ''.join(result)
return result[:-1]
`
It works for most cases. However, when I pass a string like Abcda. It fails to return the expected output, in this case, A-Bb-Ccc-Dddd-Aaaaa.
How should I go about solving this?
Thank you for taking the time to answer this.
You can do it in a much simpler way using list comprehension and enumerate
>>> s = 'abcd'
>>> '-'.join([c.upper() + c.lower()*i for i,c in enumerate(s)])
'A-Bb-Ccc-Dddd'
If you want to make your own code work, you'll just need to convert the result list to string outside your second for-loop:
def mumbler(s):
chars = list(s)
mumbled = []
result = []
for char in chars:
caps = char.upper()
num = chars.index(char)
low = char.lower()
mumbled.append( caps+ low*num)
for i in mumbled:
result.append(i+'-')
result = ''.join(result)
return result[:-1]
mumbler('Abcda')
'A-Bb-Ccc-Dddd-Aaaaa'
Go for a simple 1-liner - next() on count for maintaining the times to repeat and title() for title-casing:
from itertools import count
s = 'Abcda'
i = count(1)
print('-'.join([(x * next(i)).title() for x in s]))
# A-Bb-Ccc-Dddd-Aaaaa
I am trying to match two strings sequentially till the first the non-matched character and then determine the percentage exact match. My code is like this:
def match(a, b):
a, b = list(a), list(b)
count = 0
for i in range(len(a)):
if (a[i]!= b[i]): break
else: count = count + 1
return count/len(a)
a = '354575368987943'
b = '354535368987000'
c = '354575368987000'
print(match(a,b)) # return 0.267
print(match(a,c)) # return 0.8
Is there any built-in method in python already which can do it faster ? For simplicity assume that both strings are of same length.
There's no built-in to do the entire thing, but you can use a built-in for computing the common prefix:
import os
def match(a, b):
common = os.path.commonprefix([a, b])
return float(len(common))/len(a)
I don't think there is such build-in method.
But you can improve your implementation:
No need to wrap the inputs in list(...). Strings are indexable.
No need for count variable, i already carries the same meaning. And you can return immediately when you know the result.
Like this, with some doctests added as a bonus:
def match(a, b):
"""
>>> match('354575368987943', '354535368987000')
0.26666666666666666
>>> match('354575368987943', '354575368987000')
0.8
>>> match('354575368987943', '354575368987943')
1
"""
for i in range(len(a)):
if a[i] != b[i]:
return i / len(a)
return 1
alternative
(Just now saw that the answer below me thought of the same thing while I was editing the post)
def match(l1, l2):
# find mismatch
try:
stop = next(i for i, (el1, el2) in enumerate(zip(l1, l2)) if el1 != el2)
return stop/len(l1)
except StopIteration:
return 1
I'm trying to make a function, f(x), that would add a "-" between each letter:
For example:
f("James")
should output as:
J-a-m-e-s-
I would love it if you could use simple python functions as I am new to programming. Thanks in advance. Also, please use the "for" function because it is what I'm trying to learn.
Edit:
yes, I do want the "-" after the "s".
Can I try like this:
>>> def f(n):
... return '-'.join(n)
...
>>> f('james')
'j-a-m-e-s'
>>>
Not really sure if you require the last 'hyphen'.
Edit:
Even if you want suffixed '-', then can do like
def f(n):
return '-'.join(n) + '-'
As being learner, it is important to understand for your that "better to concat more than two strings in python" would be using str.join(iterable), whereas + operator is fine to append one string with another.
Please read following posts to explore further:
Any reason not to use + to concatenate two strings?
which is better to concat string in python?
How slow is Python's string concatenation vs. str.join?
Also, please use the "for" function because it is what I'm trying to learn
>>> def f(s):
m = s[0]
for i in s[1:]:
m += '-' + i
return m
>>> f("James")
'J-a-m-e-s'
m = s[0] character at the index 0 is assigned to the variable m
for i in s[1:]: iterate from the second character and
m += '-' + i append - + char to the variable m
Finally return the value of variable m
If you want - at the last then you could do like this.
>>> def f(s):
m = ""
for i in s:
m += i + '-'
return m
>>> f("James")
'J-a-m-e-s-'
text_list = [c+"-" for c in text]
text_strung = "".join(text_list)
As a function, takes a string as input.
def dashify(input):
output = ""
for ch in input:
output = output + ch + "-"
return output
Given you asked for a solution that uses for and a final -, simply iterate over the message and add the character and '-' to an intermediate list, then join it up. This avoids the use of string concatenations:
>>> def f(message)
l = []
for c in message:
l.append(c)
l.append('-')
return "".join(l)
>>> print(f('James'))
J-a-m-e-s-
I'm sorry, but I just have to take Alexander Ravikovich's answer a step further:
f = lambda text: "".join([c+"-" for c in text])
print(f('James')) # J-a-m-e-s-
It is never too early to learn about list comprehension.
"".join(a_list) is self-explanatory: glueing elements of a list together with a string (empty string in this example).
lambda... well that's just a way to define a function in a line. Think
square = lambda x: x**2
square(2) # returns 4
square(3) # returns 9
Python is fun, it's not {enter-a-boring-programming-language-here}.
I'm just curious if there is a simpler way to do this. If I want to print a list of items on one line I simply write
for i in things:
print i,
but if I substitute print for return I'm obviously only going to get the first item of the list. I needed the list to comma and space separated as well so I ended up with a function that looks like this
def returner(things):
thing = ""
n = 1
for i in things:
thing += i
if n < len(things):
thing += ", "
n += 1
return thing
Was there a better way to do this?
Use join
return ", ".join([str(x) for x in things])
You can use the string join function -
",".join(things)
I think you are confusing identity with representation
def returner(things): #misnomer since this is actually a generator
for itm in things:
yield itm
print ", ".join(map(str,returner(a_list)))
I'm just starting to learn python and I have this exercise that's puzzling me:
Create a function that can pack or unpack a string of letters.
So aaabb would be packed a3b2 and vice versa.
For the packing part of the function, I wrote the following
def packer(s):
if s.isalpha(): # Defines if unpacked
stack = []
for i in s:
if s.count(i) > 1:
if (i + str(s.count(i))) not in stack:
stack.append(i + str(s.count(i)))
else:
stack.append(i)
print "".join(stack)
else:
print "Something's not quite right.."
return False
packer("aaaaaaaaaaaabbbccccd")
This seems to work all proper. But the assignment says that
if the input has (for example) the letter a after b or c, then
it should later be unpacked into it's original form.
So "aaabbkka" should become a3b2k2a, not a4b2k2.
I hence figured, that I cannot use the "count()" command, since
that counts all occurrences of the item in the whole string, correct?
What would be my options here then?
On to the unpacking -
I've thought of the basics what my code needs to do -
between the " if s.isalpha():" and else, I should add an elif that
checks whether or not the string has digits in it. (I figured this would be
enough to determine whether it's the packed version or unpacked).
Create a for loop and inside of it an if sentence, which then checks for every element:
2.1. If it has a number behind it > Return (or add to an empty stack) the number times the digit
2.2. If it has no number following it > Return just the element.
Big question number 2 - how do I check whether it's a number or just another
alphabetical element following an element in the list? I guess this must be done with
slicing, but those only take integers. Could this be achieved with the index command?
Also - if this is of any relevance - so far I've basically covered lists, strings, if and for
and I've been told this exercise is doable with just those (...so if you wouldn't mind keeping this really basic)
All help appreciated for the newbie enthusiast!
SOLVED:
def packer(s):
if s.isalpha(): # Defines if unpacked
groups= []
last_char = None
for c in s:
if c == last_char:
groups[-1].append(c)
else:
groups.append([c])
last_char = c
return ''.join('%s%s' % (g[0], len(g)>1 and len(g) or '') for g in groups)
else: # Seems to be packed
stack = ""
for i in range(len(s)):
if s[i].isalpha():
if i+1 < len(s) and s[i+1].isdigit():
digit = s[i+1]
char = s[i]
i += 2
while i < len(s) and s[i].isdigit():
digit +=s[i]
i+=1
stack += char * int(digit)
else:
stack+= s[i]
else:
""
return "".join(stack)
print (packer("aaaaaaaaaaaabbbccccd"))
print (packer("a4b19am4nmba22"))
So this is my final code. Almost managed to pull it all off with just for loops and if statements.
In the end though I had to bring in the while loop to solve reading the multiple-digit numbers issue. I think I still managed to keep it simple enough. Thanks a ton millimoose and everyone else for chipping in!
A straightforward solution:
If a char is different, make a new group. Otherwise append it to the last group. Finally count all groups and join them.
def packer(s):
groups = []
last_char = None
for c in s:
if c == last_char:
groups[-1].append(c)
else:
groups.append([c])
last_char = c
return ''.join('%s%s'%(g[0], len(g)) for g in groups)
Another approach is using re.
Regex r'(.)\1+' can match consecutive characters longer than 1. And with re.sub you can easily encode it:
regex = re.compile(r'(.)\1+')
def replacer(match):
return match.group(1) + str(len(match.group(0)))
regex.sub(replacer, 'aaabbkka')
#=> 'a3b2k2a'
I think You can use `itertools.grouby' function
for example
import itertools
data = 'aaassaaasssddee'
groupped_data = ((c, len(list(g))) for c, g in itertools.groupby(data))
result = ''.join(c + (str(n) if n > 1 else '') for c, n in groupped_data)
of course one can make this code more readable using generator instead of generator statement
This is an implementation of the algorithm I outlined in the comments:
from itertools import takewhile, count, islice, izip
def consume(items):
from collections import deque
deque(items, maxlen=0)
def ilen(items):
result = count()
consume(izip(items, result))
return next(result)
def pack_or_unpack(data):
start = 0
result = []
while start < len(data):
if data[start].isdigit():
# `data` is packed, bail
return unpack(data)
run = run_len(data, start)
# append the character that might repeat
result.append(data[start])
if run > 1:
# append the length of the run of characters
result.append(str(run))
start += run
return ''.join(result)
def run_len(data, start):
"""Return the end index of the run of identical characters starting at
`start`"""
return start + ilen(takewhile(lambda c: c == data[start],
islice(data, start, None)))
def unpack(data):
result = []
for i in range(len(data)):
if data[i].isdigit():
# skip digits, we'll look for them below
continue
# packed character
c = data[i]
# number of repetitions
n = 1
if (i+1) < len(data) and data[i+1].isdigit():
# if the next character is a digit, grab all the digits in the
# substring starting at i+1
n = int(''.join(takewhile(str.isdigit, data[i+1:])))
# append the repeated character
result.append(c*n) # multiplying a string with a number repeats it
return ''.join(result)
print pack_or_unpack('aaabbc')
print pack_or_unpack('a3b2c')
print pack_or_unpack('a10')
print pack_or_unpack('b5c5')
print pack_or_unpack('abc')
A regex-flavoured version of unpack() would be:
import re
UNPACK_RE = re.compile(r'(?P<char> [a-zA-Z]) (?P<count> \d+)?', re.VERBOSE)
def unpack_re(data):
matches = UNPACK_RE.finditer(data)
pairs = ((m.group('char'), m.group('count')) for m in matches)
return ''.join(char * (int(count) if count else 1)
for char, count in pairs)
This code demonstrates the most straightforward (or "basic") approach of implementing that algorithm. It's not particularly elegant or idiomatic or necessarily efficient. (It would be if written in C, but Python has the caveats such as: indexing a string copies the character into a new string, and algorithms that seem to copy data excessively might be faster than trying to avoid this if the copying is done in C and the workaround was implemented with a Python loop.)