python简单题第五期
python 简单题 第五期(应该是没有拖更)
各位好,欢迎继续赏光python简单题第五期
(呵呵,你要是没有鱼币,我都不会看上一眼的)
本次题目真的是简单题,原题我用不到5分钟做出来了.
(原题?那意思,你是不是要给改一下?)
是的....
不多说了,上题目了:
Given an integer n, add a comma (",") as the ten-thousand-separator and return it in string format.
Example 1:
Input: n = 987
Output: "987"
Example 2:
Input: n = 12345
Output: "1,2345"
Example 3:
Input: n = 123456789
Output: "1,2345,6789"
Example 4:
Input: n = 0
Output: "0"
Constraints:
0 <= n < 2^31
本次要求,你愿意用啥用啥方法,面向对象,面向过程,函数式...没有限制.
但是别废话
用python语言.
选出清晰的,简洁的,最快回答的.
给出25鱼币奖励!
截止时间:2020年9月30日
def func(n):
s = str(n)[::-1]
return ','.join(''.join(s) for i in range(0, len(s), 4))[::-1] 本帖最后由 求资专用 于 2020-9-3 10:20 编辑
n = int(input('请输入一个一个0到2^31之间的数:'));
while not 0<n<2**31:
n = int(input('请输入一个0到2^31之间的数:'));
list1=[];
while n:
list1.append(n%10)
n=n//10
list1.reverse()
l1=len(list1)
numcomma=l1//4
if l1%4==0:
numcomma-=1
i=0
while numcomma:
i+=1
a=-(4*i+i-1)
list1.insert(a,',')
numcomma-=1
print('\"',sep='',end='')
for each in list1:
print(each,sep='',end='')
print('\"')
本帖最后由 Seawolf 于 2020-9-3 13:22 编辑
def test(input: int) -> str:
input = str(input)
p = len(input) - 1
count = 0
result = ''
while p >= 0:
if count != 4:
result = input + result
p -= 1
else:
result = ',' + result
count = 0
continue
count += 1
return result n = int(input('Given an integer n: '))
result = []
while True:
result.insert(0, f'{n % 10000}')
if n < 10000:
break
n //= 10000
print(','.join(result)) 本帖最后由 jack_xy 于 2020-9-3 16:48 编辑
classMyInt:
def myFormat(n):
str1 =str(n)
length = len(str1)
list1 = list(str1)
for pos in range(length-3,0,-3):
list1.insert(pos,',')
it1 = iter(list1)
str2 =""
while True:
try:
char1 = next(it1)
except StopIteration:
break
str2+=char1
return str2
def __init__(self,value=0):
if type(value) == int:
self.value = MyInt.myFormat(value)
def fget(self):
return self.value
def fset(self,value):
self.value = MyInt.myFormat(value)
def fdel(self):
del self.value
x = property(fget,fset,fdel)
myint =MyInt()
n = int(input("input n="))
if type(n) == int:
myint.x = n
print("Output: %s" % (myint.x)) 本次题目真的是简单题 i = int(input("input:"))
list1 = []
while not 0<=i<2**31:
i = int(input("input:"))
while i:
list1.append(str(i % 10000))
i //= 10000
list1.reverse()
print(','.join(list1)) n = input("please input a number from 0 to 2**31: ")
list_num = list(str(n))
length = len(list_num)
num, index = divmod(length, 4)
if index != 0:
for i in range(num):
list_num.insert(5*i+index, ',')
else:
for i in range(1, num):
list_num.insert(5*i-1, ',')
print(''.join(list_num)) def add_comma():
num=list(input('n='))
t=len(num)//3
for i in range(1,t+1):
num.insert(-3-(i-1)*4,',')
return ''.join(num) 太简单了,不过请你现告诉我上面那堆字母是什么意思{:5_109:} n = int(input('请输入数值:'))
result = []
while True:
if (n // 10000 != 0):
result.insert(0, '{:0>4d}'.format(n%10000))
else:
result.insert(0,'{:}'.format(n%10000))
if n < 10000:
break
n //= 10000
print(','.join(result)) num = input("请输入一个数字: ")
listn = list(num)
width = len(listn)
for i in range(width//4):
if not (width%4 == 0 and i == width//4 - 1):
listn.insert(-4 * (i + 1) - i , ',')
num=str(''.join(listn))
print(num) 心思 都是高手! 本帖最后由 afula 于 2020-9-30 10:50 编辑
def cal(num):
if len(str(num)) <= 4:
return str(num)
else:
return str(cal(num // 10000)) + "," + str(num % 10000)
num = int(input("请输入一个数值(0-2^31):"))
print(cal(num))
页:
[1]