问:

【基础题】:给一个不多于 5 位的正整数,要求:一、求它是几位数,二、逆序印出各位数字
【提高题】:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上 5, 然后用和除以 10 的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

答:

基础题:给一个不多于 5 位的正整数,要求:一、求它是几位数,二、逆序印出各位数字

方法1:

num = input("请输入不大于五位数的数字:")
print("长度为:%d" % len(num))
print("反转后数字为:%s" % num[::-1])

方法2:

def f(n):
str_n = str(n)
length = len(str_n)
reversed_n = int(str_n[::-1])
return length, reversed_n


if __name__ == "__main__":
c = input("请输入不多于5位的数字:")
a, b = f(c)
print("它是%s位数,逆序为%s" % (a, b))

方法3:

x = input("请输入不多于5位的数字:")
print(len(x))

for i in range(0, len(x)).__reversed__():
print(x[i], end=" ")

方法4:

num = input("请输入不大于五位数的数字:")
length = len(num)
print("{}是{}位数".format(num, length))
print("{}逆序后的数字为:{}".format(num, num[::-1]))

方法5:

msg = input('请输入一个不多于5位的正整数>>>')
num = int(msg)
n = len(msg)
print("该数字是 {} 位数".format(n))
for i in range(n):
single_num = num % 10
print(single_num)
num = num // 10

方法6:

num = input("Please input a num:")
print("长度是:{0}".format(len(num)))
print("倒序数字是:{0}".format(num[::-1]))

方法7:

num = input("输入一个不大于5位的正整数:")
print("{}是一个{}位数,倒数打印{}".format(num,len(num),num[::-1]))

方法8:

number = input("Enter a number: ")
print(f'Number has got {len(number)} digits')
print(f'Reversed Number is {number[::-1]}')

提高题:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上 5, 然后用和除以 10 的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

方法1:

telephone_num = input("加密前的数据:")
telephone_num_new = []
for number in telephone_num:
int_num = int(number)
int_num = (int_num + 5) % 10
telephone_num_new.append(int_num)

telephone_num_new[0], telephone_num_new[3] = telephone_num_new[3], telephone_num_new[0]
telephone_num_new[1], telephone_num_new[2] = telephone_num_new[2], telephone_num_new[1]

number_str = ""

for number_new in telephone_num_new:
number_str = number_str + str(number_new)

print("加密后的数据:", number_str)

方法2:

number = input('Enter data to be encryted: ')
basic_element = list(range(10))
dictionary = dict(zip(basic_element, basic_element[5:] + basic_element[:5]))

encrypted_number = [str(dictionary[int(digit)]) for digit in number]

encrypted_number[0],encrypted_number[3], encrypted_number[1],encrypted_number[2] = \
encrypted_number[3],encrypted_number[0], encrypted_number[2],encrypted_number[1]

encrypted_number = ''.join(encrypted_number)
print(f'Encrypted number: {encrypted_number}')

方法3:

while True:
tele_num = input("输入电话号码:")
if len(tele_num) == 4: # 判断输入字符串位数
try:
if int(tele_num) >= 0: # 判断是否为4位整数
break
except:
pass
out_tele_num = []
for num in tele_num:
num = (int(num) + 5) % 10
out_tele_num.append(num)
for i in out_tele_num:
print(i, end='')
print('')
print("{}".format(''.join(map(str, out_tele_num[::-1]))))

Python【每日一问】29的更多相关文章

  1. [python每日一练]--0012:敏感词过滤 type2

    题目链接:https://github.com/Show-Me-the-Code/show-me-the-code代码github链接:https://github.com/wjsaya/python ...

  2. Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词

    #coding:utf-8 import os,re path = 'test' files = os.listdir(path) def count_word(words): dic = {} ma ...

  3. python每日一函数 - divmod数字处理函数

    python每日一函数 - divmod数字处理函数 divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: ...

  4. 每日一问:Android 消息机制,我有必要再讲一次!

    坚持原创日更,短平快的 Android 进阶系列,敬请直接在微信公众号搜索:nanchen,直接关注并设为星标,精彩不容错过. 我 17 年的 面试系列,曾写过一篇名为:Android 面试(五):探 ...

  5. 每日一问:谈谈 volatile 关键字

    这是 wanAndroid 每日一问中的一道题,下面我们来尝试解答一下. 讲讲并发专题 volatile,synchronize,CAS,happens before, lost wake up 为了 ...

  6. 每日一问:讲讲 Java 虚拟机的垃圾回收

    昨天我们用比较精简的文字讲了 Java 虚拟机结构,没看过的可以直接从这里查看: 每日一问:你了解 Java 虚拟机结构么? 今天我们必须来看看 Java 虚拟机的垃圾回收算法是怎样的.不过在开始之前 ...

  7. 每日一问:你了解 Java 虚拟机结构么?

    对于从事 C/C++ 程序员开发的小伙伴来说,在内存管理领域非常头疼,因为他们总是需要对每一个 new 操作去写配对的 delete/free 代码.而对于我们 Android 乃至 Java 程序员 ...

  8. 每日一问:LayoutParams 你知道多少?

    前面的文章中着重讲解了 View 的测量流程.其中我提到了一句非常重要的话:View 的测量匡高是由父控件的 MeasureSpec 和 View 自身的 `LayoutParams 共同决定的.我们 ...

  9. 每日一问:简述 View 的绘制流程

    Android 开发中经常需要用一些自定义 View 去满足产品和设计的脑洞,所以 View 的绘制流程至关重要.网上目前有非常多这方面的资料,但最好的方式还是直接跟着源码进行解读,每日一问系列一直追 ...

  10. python每日一练:0007题

    第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. # -*- coding:utf-8 -*- import os def count ...

随机推荐

  1. 阿里云Centos7用putty ssh链接掉线

    解决方法:修改云服务器 ssh 配置文件(修改前一定要先备份) (1)打开配置文件: vim /etc/ssh/sshd_config 1 (2)找到下面两行: ClientAliveInterval ...

  2. 同步fifo与异步fifo

    参考以下帖子: https://blog.csdn.net/hengzo/article/details/49683707 https://blog.csdn.net/Times_poem/artic ...

  3. android studio学习---Lint工具

    对代码进行测试是一回事,但同样重要的是.我们还需要在编写代码的同时引入各种最佳实践.这不仅能够显著改进性能表现,也能增加应用程序的整体稳定性.另外,经过合理结构调整的项目在维护方面也更为轻松. And ...

  4. 【LINQ】Select与SelectMany的区别

    Select() 和 SelectMany() 的工作都是依据源值生成一个或多个结果值.Select() 为每个源值生成一个结果值.因此,总体结果是一个与源集合具有相同元素数目的集合.与之相反,Sel ...

  5. There is already an open DataReader associated with this Command which must be closed first

    通常出现在嵌套查询数据库(比如在一个qry的遍历时,又进行了数据库查询) 通过在连接字符串中允许MARS可以轻松解决这个问题. 将MultipleActiveResultSets = true添加到连 ...

  6. 使用 Spring 配置动态数据源实现读写分离

    关键词:DataSource .AbstractRoutingDataSource.AOP 首先是配置数据源 <!--读数据源配置--><bean id="readData ...

  7. ant笔记

    目录 ant远程部署 ant的使用,命令! 参考文献: ant+maven一键打包springboot上传服务器发布 判断linux文件.文件夹是否存在 shell中脚本参数传递的两种方式 shell ...

  8. 《linux就该这么学》课堂笔记19 iSCSI、MariaDB、无人值守安装

    1.iSCSI技术介绍 硬盘是计算机硬件设备中重要的组成部分之一,硬盘存储设备读写速度的快慢也会对服务器的整体性能造成影响. 为了进一步提升硬盘存储设备的读写速度和性能,人们一直在努力改进物理硬盘设备 ...

  9. STM32系列芯片命名规范

    1.STM32的基础知识 STM32是意法半导体公司,基于ARM Cortex®-M0,M0+,M3, M4和M7内核生产的系列通用MCU.截止当前时间为止(20190515),STM32有STM32 ...

  10. AI-图像基础知识-02

    目录 图像坐标系 图像数字化 图像坐标系     在前面的数据标注文章中讲述如何进行标注,而标注后会保留4个坐标点,那么这些坐标点如何表示在图片中的位置?要表示一个点或图形的位置,就需要涉及到坐标系的 ...