I wish to write a Python program that gets a number from the keyboard, and outputs a number built from the minimum of each pair of digits of the original number. For example:
Enter number please: 84372216
Original number: 84372216
New number: 4321
I had an idea to transform the original number into a string, to create a list of digits (chars) in the odd locations and the even locations, and then to compare them by the index of the lists. This doesn't sound too efficient or easy to implement, as it involves too many conversions from int to string and vice versa. The advantage is that this solves easily the problem of a number with odd number of digits.
Is there a way of solving this problem without using strings ?
Thank you.
Use zip()
on slices of the original number and min()
on each tuple
returned:
>>> number = input('Enter number please: ')
Enter number please: 84372216
>>> print('Original number: {}'.format(number))
Original number: 84372216
>>> new_number = ''.join(min(tup) for tup in zip(number[::2], number[1::2]))
>>> print('New number: {}'.format(new_number))
New number: 4321
num = 84372216
int(''.join(min(i) for i in zip(*[iter(str(num))]*2)))
#4321
list(zip(*[iter(str(num))]*2))
splits num into pairs as below
[('8', '4'), ('3', '7'), ('2', '2'), ('1', '6')]
Then use min
and join
combination to get the output
Transhuman's answer is cool, but here's a more readable version:
s = str(num)
r = int(''.join(min(s[i:i+2]) for i in range(0, len(s), 2)))
Another nifty trick is to use re.findall
:
r = int(''.join(min(i) for i in re.findall('..', str(num))))
EDIT
I didn't notice you said "without using strings". In that case:
def f(n):
return n if n < 10 else min(n % 10, n // 10 % 10) + 10 * f(n // 100)
It's quick and dirty, but it works:
def pairmin( n ):
result = 0
mult = 1
while n > 0:
if n > 10:
result = min( n % 10, (n / 10) % 10 ) * mult + result
else:
result = n * mult + result
n = n / 100
mult = mult * 10
return result
print( pairmin( 84372216 ) )
print( pairmin( 8372216 ) )
A slight variation of what Vaultah posted in comment above. You can use map
function, supply it with consecutive pairs and determine minimum from it.
s = "84372216"
"".join(list(map(lambda x,y: str(min([x,y])), s[::2],s[1::2])))
#'4321'
Here map(lambda x, y: str(min([x,y]))
takes two arguments from iterable s[::2],s[1::2]
and determine what is the minimum value from the supplied args. Min result is being cast to str
to join the final result. You can however cast the result back to int
if you require further processing of result. Just add int()
to overall expression.
To unserstand s[::2],s[1::2]
, please check Understand Python Slice Notation . Slice notation works like s[start:end:step]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.