lintcode python语法学习

按难易程度排序选择练习

一、入门

1.2097 · 求两个参数的和

题目要求:请编写 Python 代码,从 a_plus_b.py 导入函数 plus 到 main.py 下,并从命令中读取两个参数,然后从 a_plus_b 调用参数并输出相加的和,最后打印出结果。
请在 main.py 中编写相关的 Python 代码,以用来完成调用函数并且实现两个参数的相加。
import sys
# try to import the function plus from a_plus_b.py in the same directory
# - write your code here -
from mooc.lintcode.a_plus_b import plus
# read two parameters from command like `python main.py 1 2` by sys.argv
# - write your code here -
a = int(sys.argv[1])  # sys.argv[1]表示运行命令的第一个参数
b = int(sys.argv[2])  # sys.argv[2]表示运行命令的第二个参数
# call plus function and add them up
# - write your code here -
sum1 =plus(a, b)
# print the sum to the console
# - write your code here -
print(sum1)

2.2098 · 读取文件中的值并求和

题目要求:请在 main.py 中编写相关的 Python 代码,从给定的路径 input_filepath 中读取文件中的内容,然后对其进行计算求和,最后将运算结果打印出来。

import sys

input_filepath = sys.argv[1]
# write your code here to read file content
# and print it to console
with open(input_filepath) as f:
    l = f.read().split(' ')
    a = int(l[0])
    b = int(l[1])
print(a+ b)

3.2105 · 在文件中写入 Hello World!

题目要求:请在 write_hello_world.py 文件中编写相关的 Python 代码,使用 open() 函数,从给定文件路径 filepath 读取文件内容,并将 'Hello World!' 写入文件。

def write_to_file(filepath):
    # write your code here
    # you code should write "Hello World!" to the give filepath
    # do not change the function name
    with open(filepath,'w') as f:
        f.write('Hello World!')

 4.2106 · 创建文件目录并写入 Hello World!

题目要求:

请编写 Python 代码,在可能没有文件目录的情况下,导入 os 库并创建一个文件目录,将 'Hello World!' 写入新创建的文件当中。

请在 write_hello_world.py 文件中编写相关的 Python 代码,以实现创建一个新的文件目录,并将 'Hello World!' 写入其中。

import os 
def write_to_file(filepath):
    if os.path.isfile(filepath):  # 判断目录文件是否存在
        with open(filepath,'w') as f:
            f.write('Hello World!')
    else:
        path = os.path.dirname(filepath)  # 获取目录路径
        try:
            os.mkdir(path)  # 新建目录
        except:
            pass
        finally:
            with open(filepath,'w') as f:
                f.write('Hello World!')

5.2127 · 实现列表内数字的倒序排序

题目要求:请在 solution.py 里完善代码,实现 list_sort 函数功能,list_sort 函数有一个参数 list_in,请将参数 list_in 里面的数字倒序排序并返回。我们会在 main.py 里导入你在 solution.py 中完善的代码并运行,如果你的代码逻辑正确且运行成功,程序会返回一个列表作为运算后的结果。

def list_sort(list_in: list) -> list:
    """
    :param list_in: The first input list
    :return: A list sorted from largest to smallest
    """
    # write your code here
    list_in.sort(reverse=True)
    return list_in

6.2257 · 判断是否为素数

题目要求:请编写 Python 代码,实现名为 prime 的函数。该函数将判断传入整数 num 是否为素数,如果是素数则返回 True,不是则返回 False
请在 solution.py 中编写 prime 函数的代码,我们会在 main.py 中通过导入的方式运行你的代码,以检测是否正确完成以上功能。

def prime(num: int) -> int:
    """
    :param num: a random integer
    :return: determine if the result is a prime number and return 1 otherwise other values
    """
	# write your code here
    count = True
    if num == 1 or num == 0:
        return False
    else:
        for i in range(2,num):
            if num % i ==0:
                count = False
                break
        return count

7.2258 · 倒着寻找最大素数

题目要求:

本题主要目的是我们会提供一个大于 1 的自然数,然后判断这个数是否为素数,如果为素数,则直接将其打印,如果不是素数,寻找前一位,则找到的第一个素数即为最大素数。

请在 solution.py 中编写相关的 Python 代码,将其生成的最大素数打印出来。

def Find_prime_numbers_backwards(n: int) -> int:
    '''
    :param n: Natural number greater than 1
    :return: Maximum prime number
    '''
    # write your code here
    for i in range(n, 1, -1):
        count = True
        for j in range(2, i):
            if i % j == 0:
                count = False
                break
        if count:
            print(i)
            break

8.2286 · 求最大公约数(Python 版)

题目要求:给出两个整数 a 和 b,请计算 a 和 b 的最大公约数,通过 print 语句输出。

import sys

a = int(sys.argv[1])
b = int(sys.argv[2])
# write your code here
# please print the greatest common divisor of a and b
for i in range(min(a,b),0,-1):
    if a % i == 0 and b % i == 0:
        print(i)
        break

9.2340 · 计算小写字母个数(Python 版)

题目要求:给定一个由大写字母和小写字母组成的字符串 s,输出 s 中小写字母的个数。

学习点:ord()函数和chr()函数

ord('a')返回小写字母a在ASCII码表中第几个,chr(97)返回ASCII码表中第97个字符

26位小写字母是97~122,26位大写字母是65~90

s = input()
# output the answer to the console according to the requirements of the question
count = 0
for i in s:
    if ord(i) in range(97, 123):
        count += 1
print(count)

10.2418 · 位运算左移三位

题目要求:请从标准输入流(控制台)中获取一个自然数 n,请通过 print 语句输出 n 左移三位后的结果到标准输出流(控制台)。

学习点:参考https://blog.csdn.net/weixin_39904268/article/details/112371613

1、与运算通常应用的场景是获取某一位的值为1还是0(如判断奇数偶数,统计数值中1的个数);

2、左移右移特性:左移一位相当于乘以2,右移一位相当于除以2;

3、异或特性:任意数和自身异或结果为0;0和任意数异或结果还是其本身。

# write your code here
# read data from console
n = eval(input())
# output the answer to the console according to the requirements of the question
print(n*2**3)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于Python基本语法学习,我可以为你提供一些指导。Python是一种高级编程语言,它具有简洁明了的语法,非常适合初学者入门。 首先,Python的注释使用井号(#)开头,可以在代码中添加注释来解释代码的功能和目的。 变量是存储数据的容器,你可以使用等号(=)来给变量赋值。例如: ```python name = 'John' age = 25 ``` Python中的基本数据类型包括整数(int)、浮点数(float)、布尔值(bool)、字符串(str)等。你可以直接使用这些数据类型来操作数据。 条件语句可以帮助你根据条件执行不同的代码块。例如,使用if语句来判断某个条件是否为真,并在条件满足时执行相应的代码块。例如: ```python if age >= 18: print("成年人") else: print("未成年人") ``` 循环语句可以帮助你重复执行一段代码。Python中有两种常用的循环:for循环和while循环。例如,使用for循环遍历一个列表: ```python fruits = ['apple', 'banana', 'orange'] for fruit in fruits: print(fruit) ``` 函数是一段完成特定任务的代码块,可以重复使用。你可以使用def关键字来定义函数,并使用return语句返回结果。例如: ```python def add_numbers(a, b): return a + b result = add_numbers(3, 5) print(result) # 输出:8 ``` 这只是Python基本语法的一小部分,如果你有具体的问题或需要更深入的学习,可以告诉我。我会尽力帮助你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值