python 更多课堂练习题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 更多课堂练习题相关的知识,希望对你有一定的参考价值。

#Class practice in Python

#Problem 1
#The Turing Machine
"""The Turing Machine is a mathematical machine that has some infinite array of cells(for this example use 1000).
In each of these cells, it contains some type of number. The macine also has a read and write head component, which with commands, can move across the type, read, write, or print the contents of any cells
Your task is to implement a turing machine that takes commands in the form of strings. 
It must use the following commands:
">": Move the head one index forward
"<": Move the head one index back
"+": increment the number of the current cell by 1.
"-":decrement the number of the current cell by 1.
".":Print the contents of the current cell.
The eval method of the class should be the one to process these commands.
   d = TuringMachine()
=> None
   d.eval(".++.")
0
2
=> None
   d.eval(".++.")
2
4
=> None
   d.eval(".++--.")
4
4
=> None
   d.eval(">>++++.")
4
"""
class TuringMachine:
	
	def __init__(self):
		self.cells = [0 for i in range(1000)]
		self.head = 0
	
	def eval(self, code):
		pass
				
				
#Problem 2
#Family Tree
"""
Create a class that implements a family tree.
Here, every node in the tree is a person, whom has two parents, and one or more children.
Each node is called a "Person", and that person has some surname attribute, a string.
Each person can create a child with another person whom does not have the same surname.
Additionally, each person has some first name and a gender, "M" or "F".
=> None
   a = Person("josh", "farley", "M")
=> None
   a
=> (josh, farley) -> [[]]
   b = Person("sarah", "smith", "F")
=> None
   a.create_child(b, "sam")
=> None
   a
=> (josh, farley) -> [[(sam, farley) -> [[]]]]
   b
=> (sarah, smith) -> [[(sam, farley) -> [[]]]]
"""

class Person:
	
	def __init__(self, first, last, gender):
		assert gender in ["M", "F"]
		"""Your code here"""
		pass
	
	def __repr__(self):
		return "({0}, {1}) -> [{2}]".format(self.first, self.last, self.children)
		
	def pickgender(self):
		#facilitates a random picking of gender
		import random
		return random.choice(["M", "F"])
	
	def create_child(self, other, first):
		"""Your code here"""
		pass
				

以上是关于python 更多课堂练习题的主要内容,如果未能解决你的问题,请参考以下文章

课堂练习:Python-三级菜单

课堂练习-python 装饰器

课堂练习-python 生成器并行

九Python+Selenium模拟用QQ登陆腾讯课堂,并提取报名课程(练习)

课堂练习

课堂练习word count