美文网首页
Python 100 实例之(71 - 80)

Python 100 实例之(71 - 80)

作者: Kevin_小飞象 | 来源:发表于2020-05-17 19:20 被阅读0次

example_071.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 编写input()和output()函数输入,输出5个学生的数据记录。 '

__author__ = 'Kevin Gong'

N = 3
# stu
# num : string
# name : string
# score[4] : list

student = []
for i in range(5):
    student.append(['', '', []])

def input_stu(stu):
    for i in range(N):
        stu[i][0] = input('input student num:\n')
        stu[i][1] = input('input student name:\n')
        for j in range(3):
            stu[i][2].append(int(input('score:\n')))

def output_stu(stu):
    for i in range(N):
        print('%-6s%-10s' % (stu[i][0], stu[i][1]))
        for j in range(3):
            print('%-8d' % stu[i][2][j])

if __name__ == '__main__':
    input_stu(student)
    print(student)
    output_stu(student)

example_072.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 创建一个链表 '

__author__ = 'Kevin Gong'

class Node:

    def __init__(self, data):
        self.data = data
        self.next = None

    def get_data(self):
        return self.data

class List:

    def __init__(self, head):
        self.head = head

    def is_empty(self):
        return self.get_len() == 0

    def get_len(self):
        length = 0
        temp = self.head
        while temp is not None:
            length += 1
            temp = temp.next
        return length

    def append(self, node):
        temp = self.head
        while temp.next is not None:
            temp = temp.next
        temp.next = node

    def delete(self, index):
        if index < 1 or index > self.get_len():
            print('给定的位置不合理')
            return
        if index == 1:
            self.head = self.head.next
            return
        temp = self.head
        cur_pos = 0
        while temp is not None:
            cur_pos += 1
            if cur_pos == index - 1:
                temp.next = temp.next.next
            temp = temp.next

    def insert(self, pos, node):
        if pos < 1 or pos > self.get_len():
            print('插入节点位置不合理')
            return
        temp = self.head
        cur_pos = 0
        while temp is not Node:
            cur_pos += 1
            if cur_pos == pos - 1:
                node.next = temp.next
                temp.next = node
                break
            temp = temp.next

    def reverse(self, head):
        if head is None and head.next is None:
            return head
        pre = head
        cur = head.next
        while cur is not None:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        head.next = None
        return pre

    def print_list(self, head):
        init_data = []
        while head is not None:
            init_data.append(head.get_data())
            head = head.next
        return init_data

if __name__ == '__main__':
    head = Node('head')
    link = List(head)
    for i in range(10):
        node = Node(i)
        link.append(node)
    print(link.print_list(head))
    

example_073.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 反向输出链表 '

__author__ = 'Kevin Gong'

class Node:

    def __init__(self, data):
        self.data = data
        self.next = None

    def get_data(self):
        return self.data

class List:

    def __init__(self, head):
        self.head = head

    def is_empty(self):
        return self.get_len() == 0

    def get_len(self):
        length = 0
        temp = self.head
        while temp is not None:
            length += 1
            temp = temp.next
        return length

    def append(self, node):
        temp = self.head
        while temp.next is not None:
            temp = temp.next
        temp.next = node

    def delete(self, index):
        if index < 1 or index > self.get_len():
            print('给定的位置不合理')
            return
        if index == 1:
            self.head = self.head.next
            return
        temp = self.head
        cur_pos = 0
        while temp is not None:
            cur_pos += 1
            if cur_pos == index - 1:
                temp.next = temp.next.next
            temp = temp.next

    def insert(self, pos, node):
        if pos < 1 or pos > self.get_len():
            print('插入节点位置不合理')
            return
        temp = self.head
        cur_pos = 0
        while temp is not Node:
            cur_pos += 1
            if cur_pos == pos - 1:
                node.next = temp.next
                temp.next = node
                break
            temp = temp.next

    def reverse(self, head):
        if head is None and head.next is None:
            return head
        pre = head
        cur = head.next
        while cur is not None:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        head.next = None
        return pre

    def print_list(self, head):
        init_data = []
        while head is not None:
            init_data.append(head.get_data())
            head = head.next
        return init_data

if __name__ == '__main__':
    head = Node('head')
    link = List(head)
    for i in range(10):
        node = Node(i)
        link.append(node)
    print(link.print_list(head))
    print(link.print_list(link.reverse(head)))
    

example_074.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 列表排序及连接 '

__author__ = 'Kevin Gong'

a = [2, 6, 8]
b = [7, 0, 4]
a.extend(b)
a.sort()
print(a)

example_075.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 放松一下,算一道简单的题目 '

__author__ = 'Kevin Gong'

if __name__ == '__main__':
    for i in range(5):
        n = 0
        if i != 1:
            n += 1
        if i == 3:
            n += 1
        if i == 4:
            n += 1
        if i != 4:
            n += 1
        if n == 3:
            print(64 + i)

example_076.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n '

__author__ = 'Kevin Gong'

def peven(n):
    i = 0
    s = 0.0
    for i in range(2, n + 1, 2):
        s += 1.0 / i
    return s

def podd(n):
    s = 0.0
    for i in range(1, n + 1, 2):
        s += 1.0 / i   # Python里,整数除整数,只能得出整数,所以需要使用 浮点数 1.0
    return s

def dcall(fp, n):
    s = fp(n)
    return s

if __name__ == '__main__':
    n = int(input('input a number: '))
    if n % 2 == 0:
        sum = dcall(peven, n)
    else:
        sum = dcall(podd, n)
    print(sum)

example_077.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 循环输出列表 '

__author__ = 'Kevin Gong'

raw = ['apple', 'orange', 'pich', 'banana','1024']
for i in range(len(raw)):
    print(raw[i])

example_078.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 找到年龄最大的人,并输出。 '

__author__ = 'Kevin Gong'

if __name__ == '__main__':
    person = {'Kevin':29, 'Scarf':18, 'Sun':40, 'Cidy':3}
    m = 'Kevin'
    for key in person.keys():
        if person[m] < person[key]:
            m = key
    
    print('姓名:%s, 年龄:%d' % (m, person[m]))

example_079.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 字符串排序 '

__author__ = 'Kevin Gong'

raw = ['abcde', 'efdis', 'adk']
raw.sort()
print(raw)

example_080.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 猴子分桃 '

__author__ = 'Kevin Gong'

if __name__ == '__main__':
    i = 0
    j = 1
    x = 0
    while i < 5:
        x = 4 * j
        for i in range(0, 5):
            if x % 4 != 0:
                break
            else:
                i += 1
            x = (x / 4) * 5 + 1
        j += 1
    print(x)


相关文章

网友评论

      本文标题:Python 100 实例之(71 - 80)

      本文链接:https://www.haomeiwen.com/subject/asyrohtx.html