【Python Practice】Day 23 Question 100-103

'''
@Author: your name
@Date: 2020-07-30 12:21:21
@LastEditTime: 2020-07-30 13:48:41
@LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: \vscode_py\day24.py
'''
# Question 100
# You are given words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification.

# If the following string is given as input to the program:
# 使用list存放无重复的字符串,使用dict存放数量
def Q100():
    word_list=[]
    word_dict={}
    n=int(input("num:"))

    for i in range(n):
        word=input("time:{},str: ".format(i+1))
        if word not in word_list:
            word_list.append(word)
        word_dict[word]=word_dict.get(word,0)+1

    print("len:{}".format(len(word_list)))
    for word in word_list:
        print(word_dict[word],end=" ")

# Question 101
# 统计字符串中的字母数量
def Q101():
    word=input("")
    dic={}
    for i in word:
        dic[i]=dic.get(i,0)+1
    
    # 排序
    dct=sorted(dic.items(),key=lambda x:(-x[1],x[0]))
    for i in dct:
        print(i[0],i[1])


# Question 102
# Write a Python program that accepts a string and calculate the number of digits and letters.
def Q102():
    word=input()
    a,b=0,0
    for i in word:
        a+=i.isdigit()
        b+=i.isalpha()
    
    print("Digit -",a)
    print("Letter -",b)


# Question 103
# Given a number N.Find Sum of 1 to N Using Recursion
def Q103():
    n=int(input("n:"))

    # method 1
    l=[i for i in range(1,n+1)]
    ans=sum(l)



    print(ans)

# method 2
# 递归
def add_to(n):
    if n==0:
        return n
    else:
        return add_to(n-1)+n

    
    



if __name__ == "__main__":
    # Q100()

    # Q101()

    # Q102()


    # Q103()

    n=int(input("n:"))
    ans=add_to(n)
    print(ans)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值