当前位置: 首页 > news >正文

网站开发后台php技术专业的网络推广

网站开发后台php技术,专业的网络推广,广南网站建设,王也头像图片文章目录 问题1:坐标轴及标签显示问题2:赢得比赛的最佳项目并计数问题3:21点游戏方法1:把超过21点的最后记为0方法2:把超过21点在最后进行判断方法3:官方代码与方法2相似 问题1:坐标轴及标签显示…

文章目录

    • 问题1:坐标轴及标签显示
    • 问题2:赢得比赛的最佳项目并计数
    • 问题3:21点游戏
      • 方法1:把超过21点的最后记为0
      • 方法2:把超过21点在最后进行判断
      • 方法3:官方代码与方法2相似

问题1:坐标轴及标签显示

正如你所看到的,他最近运气不佳。他想用一些精选的表情符号沿着在推特上发布这条消息,但是,就像现在看起来的那样,他的追随者可能会觉得很困惑。他问你是否可以帮助他做出以下改变:

添加标题“500次老虎机抽奖的结果”
使y轴从0开始。
将标签“Balance”添加到y轴
在调用type(graph)之后,您可以看到Jimmy的图是类型 matplotlib.axes._subplots.AxesSubplot 。嗯,这是新的。通过调用dir(graph),您会发现三个看起来很有用的方法:.set_title()、.set_ylim()和.set_ylabel()。

使用这些方法根据Jimmy的请求完成函数prettify_graph。我们已经为您检查了第一个请求(设置标题)。

(记住:如果你不知道这些方法的作用,请使用help()函数!)

  • .set_title():设置标题
  • .set_ylim():设置y轴
  • .set_ylabel():设置y轴标签
def prettify_graph(graph):"""Modify the given graph according to Jimmy's requests: add a title, make the y-axisstart at 0, label the y-axis. (And, if you're feeling ambitious, format the tick marksas dollar amounts using the "$" symbol.)"""graph.set_title("Results of 500 slot machine pulls")# Complete steps 2 and 3 here
#     graph.set_title("Results of 500 slot machine pulls")graph.set_ylim(0)graph.set_ylabel("Balance")graph = jimmy_slots.get_graph()
prettify_graph(graph)
graph

在这里插入图片描述
额外好处:您可以对y轴上的数字进行格式设置,使它们看起来像美元金额吗?例如,200美元而不是200美元。

def prettify_graph(graph):graph.set_title("Results of 500 slot machine pulls")# Make the y-axis begin at 0graph.set_ylim(bottom=0)# Label the y-axisgraph.set_ylabel("Balance")# Bonus: format the numbers on the y-axis as dollar amounts# An array of the values displayed on the y-axis (150, 175, 200, etc.)ticks = graph.get_yticks()# Format those values into strings beginning with dollar signnew_labels = ['${}'.format(int(amt)) for amt in ticks]# Set the new labelsgraph.set_yticklabels(new_labels)graph = jimmy_slots.get_graph()
prettify_graph(graph)
graph

在这里插入图片描述

问题2:赢得比赛的最佳项目并计数

Luigi正试图进行分析,以确定在Mario Kart赛道上赢得比赛的最佳项目。他有一些字典列表形式的数据,看起来像…

[
{‘name’: ‘Peach’, ‘items’: [‘green shell’, ‘banana’, ‘green shell’,], ‘finish’: 3},
{‘name’: ‘Bowser’, ‘items’: [‘green shell’,], ‘finish’: 1},
# Sometimes the racer’s name wasn’t recorded
{‘name’: None, ‘items’: [‘mushroom’,], ‘finish’: 2},
{‘name’: ‘Toad’, ‘items’: [‘green shell’, ‘mushroom’], ‘finish’: 1},
]
“items”是参赛者在比赛中获得的所有能量提升物品的列表,“finish”是他们在比赛中的位置(1代表第一名,3代表第三名,等等)。

他写了下面的函数来获取这样的列表,并返回一个字典,将每个条目映射到第一名完成者拾取的次数。

def best_items(racers):# 创建空字典用于存储第一名完成者拾取的次数winner_item_counts = {}for i in range(len(racers)):# 对于列表中每个字典进行操作racer = racers[i]# 如果是第一名if racer['finish'] == 1:# 获取第一名的项目for item in racer['items']:# 如果结果字典中没有该元素,进行创建if item not in winner_item_counts:winner_item_counts[item] = 0# 有该元素则次数加1winner_item_counts[item] += 1# 如果名字未记录,发出警告if racer['name'] is None:print("警告:{}/{}名字为空,未记录! (racer = {})".format((i+1),len(racers),racer['name']))return winner_item_counts sample = [{'name': 'Peach', 'items': ['green shell', 'banana', 'green shell',], 'finish': 3},{'name': 'Bowser', 'items': ['green shell',], 'finish': 1},{'name': None, 'items': ['mushroom',], 'finish': 2},{'name': 'Toad', 'items': ['green shell', 'mushroom'], 'finish': 1},
]
best_items(sample)

问题3:21点游戏

假设我们想创建一个新类型来表示21点中的手牌。我们可能想对这种类型做的一件事是重载比较运算符,如>和<=,以便我们可以使用它们来检查一只手是否击败另一只手。如果我们能做到这一点,那就太酷了:

hand1 = BlackjackHand([‘K’, ‘A’])
hand2 = BlackjackHand([‘7’, ‘10’, ‘A’])
hand1 > hand2
True

好吧,我们不会在这个问题中完成所有这些(定义自定义类有点超出了这些课程的范围),但是我们要求您在下面的函数中编写的代码与我们定义自己的BlackjackHand类时必须编写的代码非常相似。(We我把它放在__gt__magic方法中,为>定义我们的自定义行为。)

根据文档字符串填充blackjack_hand_greater_than函数体

方法1:把超过21点的最后记为0

1、首先定义一个21点的计数函数BlackjackHand(hand)

def BlackjackHand(hand):count = 0A_count = 0# 进行遍历,如果为A,J,Q,K中的其中一个,加各自对应的数# 若为1~10,则加int(x)for x in hand:if x == 'A':A_count = A_count + 1count = count + 11continueif (x == 'J' or  x == 'Q' or x == 'K'):count = count + 10continueelse:count = count + int(x)# 判断        if A_count == 0 and count <= 21:count = countelif A_count == 0 and count > 21:count = 0elif A_count > 0 and count <= 21:count = countelif A_count > 0 and count > 21:for i in range(A_count):count = count - 10if count <= 21:count = countif count > 21:count = 0return count# 测试
print(BlackjackHand(['K']))
print(BlackjackHand(['3', '4']))
print(BlackjackHand(['10']))
print(BlackjackHand(['K', 'K', '2']))
print(BlackjackHand(['A', 'A', '9']))
print(BlackjackHand(['A', 'A', '9', '3']))
print(BlackjackHand(['9','Q',' 8','A']))

在这里插入图片描述
2、最终代码

def blackjack_hand_greater_than(hand_1, hand_2):"""Return True if hand_1 beats hand_2, and False otherwise.In order for hand_1 to beat hand_2 the following must be true:- The total of hand_1 must not exceed 21- The total of hand_1 must exceed the total of hand_2 OR hand_2's total must exceed 21Hands are represented as a list of cards. Each card is represented by a string.When adding up a hand's total, cards with numbers count for that many points. Facecards ('J', 'Q', and 'K') are worth 10 points. 'A' can count for 1 or 11.When determining a hand's total, you should try to count aces in the way that maximizes the hand's total without going over 21. e.g. the total of ['A', 'A', '9'] is 21,the total of ['A', 'A', '9', '3'] is 14.Examples:>>> blackjack_hand_greater_than(['K'], ['3', '4'])True>>> blackjack_hand_greater_than(['K'], ['10'])False>>> blackjack_hand_greater_than(['K', 'K', '2'], ['3'])False"""if BlackjackHand(hand_1) > BlackjackHand(hand_2):return Trueelse:return Falsedef BlackjackHand(hand):count = 0A_count = 0for x in hand:if x == 'A':A_count = A_count + 1count = count + 11continueif (x == 'J' or  x == 'Q' or x == 'K'):count = count + 10continueelse:count = count + int(x)if A_count == 0 and count <= 21:count = countelif A_count == 0 and count > 21:count = 0elif A_count > 0 and count <= 21:count = countelif A_count > 0 and count > 21:for i in range(A_count):count = count - 10if count <= 21:count = countif count > 21:count = 0return count

方法2:把超过21点在最后进行判断

1、首先定义一个21点的计数函数BlackjackHand(hand)

def BlackjackHand(hand):count = 0A_count = 0for x in hand:if x == 'A':A_count = A_count + 1count = count + 11continueif (x == 'J' or  x == 'Q' or x == 'K'):count = count + 10continueelse:count = count + int(x)if A_count > 0 and count > 21:for i in range(A_count):count = count - 10if count <= 21:count = countif count > 21:count = countreturn count# 测试
print(BlackjackHand(['K']))
print(BlackjackHand(['3', '4']))
print(BlackjackHand(['10']))
print(BlackjackHand(['K', 'K', '2']))
print(BlackjackHand(['A', 'A', '9']))
print(BlackjackHand(['A', 'A', '9', '3']))
print(BlackjackHand(['9','Q',' 8','A']))

超过21点不变,等到最后进行判断为False
在这里插入图片描述
2、最终代码

def blackjack_hand_greater_than(hand_1, hand_2):"""如果hand_1打败hand_2返回True,否则返回False。为了使hand_1打败hand_2,以下条件必须成立:—hand_1的总数不能超过21—hand_1的总和必须大于hand_2的总和,或者hand_2的总和必须大于21手牌被表示为一张牌的列表。每张卡片由一个字符串表示。当把一手牌的总牌数加起来时,带有数字的牌就会得到相应的点数。脸卡片(“J”、“Q”和“K”)值10分。“A”可以数1或11。当决定一手牌的总数时,你应该尝试用下面的方法来计算a在不超过21的情况下最大化手牌总数。例如,['A', 'A', '9']的总数是21,['A', 'A', '9', '3']的总数是14。Examples:>>> blackjack_hand_greater_than(['K'], ['3', '4'])True>>> blackjack_hand_greater_than(['K'], ['10'])False>>> blackjack_hand_greater_than(['K', 'K', '2'], ['3'])False"""  if (BlackjackHand(hand_1)<=21) and (BlackjackHand(hand_1) > BlackjackHand(hand_2)) or (BlackjackHand(hand_1)<=21 and BlackjackHand(hand_2)>21):return Trueelse:return Falsedef BlackjackHand(hand):# 定义总数和'A'的数量count = 0A_count = 0# for x in hand:if x == 'A':A_count = A_count + 1count = count + 11continueif (x == 'J' or  x == 'Q' or x == 'K'):count = count + 10continueelse:count = count + int(x)# 判断        if A_count > 0 and count > 21:for i in range(A_count):count = count - 10if count <= 21:count = countif count > 21:count = countreturn count

方法3:官方代码与方法2相似

def hand_total(hand):"""Helper function to calculate the total points of a blackjack hand."""total = 0# 计算a的数量,并在最后处理如何使用它们aces = 0for card in hand:if card in ['J', 'Q', 'K']:total += 10elif card == 'A':aces += 1else:# Convert number cards (e.g. '7') to intstotal += int(card)# At this point, total is the sum of this hand's cards *not counting aces*.# 添加a,现在把它们记为1。这是我们从这只手能得到的最小的总数total += aces# 把a从1 “升级”到11只要它能帮我们接近21# without bustingwhile total + 10 <= 21 and aces > 0:# Upgrade an ace from 1 to 11total += 10aces -= 1return totaldef blackjack_hand_greater_than(hand_1, hand_2):total_1 = hand_total(hand_1)total_2 = hand_total(hand_2)return total_1 <= 21 and (total_1 > total_2 or total_2 > 21)

简化:

  • if card in ['J', 'Q', 'K']:
  • 在这里插入图片描述
http://www.wooajung.com/news/31225.html

相关文章:

  • wordpress 如何改中文字体seo的五个步骤
  • 厦门建设工程交易中心网站公关
  • 建设网站需要多少人什么是seo什么是sem
  • 做游戏网站赚钱吗如何在百度发布广告
  • php做的网站代码2022网站seo
  • 免费搭建淘宝客网站2023网络营销成功案例
  • 网站信息员队伍建设方案网站优化外包价格
  • 常州知名网站公司企业网络营销推广案例
  • 珠海营销型网站建设上海今天最新新闻10条
  • 门户网站建设方案知乎seo
  • 永州网站开发最厉害的搜索引擎
  • 绵阳网站建设 科雨网络刷粉网站推广马上刷
  • 做黑彩网站赚钱吗百度网址大全官方下载
  • 阿里巴巴网站怎么设计师seo快速推广
  • 五原网站建设优秀品牌策划方案
  • 做图片格式跟尺度用哪个网站好灰色词seo排名
  • 做网站收录现在最火的发帖平台
  • 推广计划方案seo排名点击首页
  • 怎么查看网站开发使用什么技术产品故事软文案例
  • wordpress调用分类id微信seo排名优化软件
  • 护士公共课在哪个网站做如何制作自己的网址
  • 网站建设的基本需求有哪些方面学生个人网页制作html
  • wordpress文章只显示摘要上海优化公司排行榜
  • 如何自己制作一个软件廊坊seo网站管理
  • 在上海做钟点工的网站企业培训课程
  • 美食教做网站计算机编程培训学校哪家好
  • 做平台网站需要多少钱企业网络
  • 学互联网做网站是什么云南百度推广开户
  • 全景图网站怎么做搜索引擎优化的作用
  • 网站的优化公司如何建网站不花钱