鱼C论坛

 找回密码
 立即注册
查看: 2188|回复: 17

[已解决]python简单题第五期

[复制链接]
发表于 2020-9-2 20:54:24 | 显示全部楼层 |阅读模式
25鱼币
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日


最佳答案
2020-9-2 20:54:25
  1. def func(n):
  2.     s = str(n)[::-1]
  3.     return ','.join(''.join(s[i:i+4]) for i in range(0, len(s), 4))[::-1]
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-2 20:54:25 | 显示全部楼层    本楼为最佳答案   
  1. def func(n):
  2.     s = str(n)[::-1]
  3.     return ','.join(''.join(s[i:i+4]) for i in range(0, len(s), 4))[::-1]
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-3 10:12:38 | 显示全部楼层
本帖最后由 求资专用 于 2020-9-3 10:20 编辑
  1. n = int(input('请输入一个一个0到2^31之间的数:'));
  2. while not 0<n<2**31:
  3.     n = int(input('请输入一个0到2^31之间的数:'));   
  4. list1=[];
  5. while n:
  6.     list1.append(n%10)
  7.     n=n//10
  8. list1.reverse()
  9. l1=len(list1)
  10. numcomma=l1//4
  11. if l1%4==0:
  12.     numcomma-=1
  13. i=0
  14. while numcomma:
  15.     i+=1
  16.     a=-(4*i+i-1)
  17.     list1.insert(a,',')
  18.     numcomma-=1
  19. print('"',sep='',end='')
  20. for each in list1:
  21.     print(each,sep='',end='')
  22. print('"')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-3 13:07:19 | 显示全部楼层
本帖最后由 Seawolf 于 2020-9-3 13:22 编辑
  1. def test(input: int) -> str:
  2.   input = str(input)
  3.   p = len(input) - 1
  4.   count = 0
  5.   result = ''
  6.   while p >= 0:
  7.     if count != 4:
  8.       result = input[p] + result
  9.       p -= 1
  10.     else:
  11.       result = ',' + result
  12.       count = 0
  13.       continue
  14.     count += 1
  15.   return result
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-3 16:36:36 | 显示全部楼层
  1. n = int(input('Given an integer n: '))
  2. result = []
  3. while True:
  4.     result.insert(0, f'{n % 10000}')
  5.     if n < 10000:
  6.         break
  7.     n //= 10000
  8. print(','.join(result))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-3 16:46:12 | 显示全部楼层
本帖最后由 jack_xy 于 2020-9-3 16:48 编辑
  1. class  MyInt:
  2.     def myFormat(n):
  3.         str1 =str(n)
  4.         length = len(str1)
  5.         list1 = list(str1)
  6.         for pos in range(length-3,0,-3):
  7.             list1.insert(pos,',')
  8.         it1 = iter(list1)
  9.         str2 =""
  10.         while True:
  11.             try:
  12.                 char1 = next(it1)
  13.             except StopIteration:
  14.                 break
  15.             str2+=char1
  16.         return str2

  17.     def __init__(self,value=0):
  18.         if type(value) == int:
  19.             self.value = MyInt.myFormat(value)

  20.     def fget(self):
  21.         return self.value

  22.     def fset(self,value):
  23.         self.value = MyInt.myFormat(value)

  24.     def fdel(self):
  25.         del self.value

  26.     x = property(fget,fset,fdel)

  27. myint =MyInt()
  28. n = int(input("input n="))
  29. if type(n) == int:
  30.    
  31.     myint.x = n
  32.     print("Output: %s" % (myint.x))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-3 17:30:15 | 显示全部楼层
本次题目真的是简单题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-3 18:01:30 | 显示全部楼层
  1. i = int(input("input:"))
  2. list1 = []
  3. while not 0<=i<2**31:
  4.     i = int(input("input:"))
  5. while i:
  6.     list1.append(str(i % 10000))
  7.     i //= 10000
  8. list1.reverse()
  9. print(','.join(list1))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-5 08:36:18 | 显示全部楼层
  1. n = input("please input a number from 0 to 2**31: ")
  2. list_num = list(str(n))
  3. length = len(list_num)
  4. num, index = divmod(length, 4)
  5. if index != 0:
  6.     for i in range(num):
  7.         list_num.insert(5*i+index, ',')
  8. else:
  9.     for i in range(1, num):
  10.         list_num.insert(5*i-1, ',')
  11. print(''.join(list_num))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-5 12:04:07 | 显示全部楼层
  1. def add_comma():
  2.         num=list(input('n='))
  3.         t=len(num)//3
  4.         for i in range(1,t+1):
  5.                 num.insert(-3-(i-1)*4,',')
  6.         return ''.join(num)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-7 09:08:59 | 显示全部楼层
太简单了,不过请你现告诉我上面那堆字母是什么意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-7 13:34:59 | 显示全部楼层
  1. n = int(input('请输入数值:'))
  2. result = []
  3. while True:
  4.     if (n // 10000 != 0):
  5.         result.insert(0, '{:0>4d}'.format(n%10000))
  6.     else:
  7.         result.insert(0,'{:}'.format(n%10000))
  8.     if n < 10000:
  9.         break
  10.     n //= 10000

  11. print(','.join(result))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-8 15:35:01 | 显示全部楼层
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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-28 00:47:31 From FishC Mobile | 显示全部楼层
心思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-28 05:13:54 | 显示全部楼层
都是高手!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2020-9-28 06:52:43 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2020-9-29 08:14:35 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-30 10:48:01 | 显示全部楼层
本帖最后由 afula 于 2020-9-30 10:50 编辑
  1. def cal(num):
  2.     if len(str(num)) <= 4:
  3.         return str(num)
  4.     else:
  5.         return str(cal(num // 10000)) + "," + str(num % 10000)
  6. num = int(input("请输入一个数值(0-2^31):"))
  7. print(cal(num))
复制代码

评分

参与人数 1荣誉 +1 鱼币 +1 贡献 +1 收起 理由
沉默的人e + 1 + 1 + 1 再想想,答案错了

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 09:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表