【Kata Daily 190908】How Much?
原题:
I always thought that my old friend John was rather richer than he looked, but I never knew exactly how much money he actually had. One day (as I was plying him with questions) he said:
- "Imagine I have between
mandnZloty..." (or did he say Quetzal? I can't remember!) - "If I were to buy 9 cars costing
ceach, I'd only have 1 Zloty (or was it Meticals?) left." - "And if I were to buy 7 boats at
beach, I'd only have 2 Ringglets (or was it Zloty?) left."
Could you tell me in each possible case:
- how much money
fhe could possibly have ? - the cost
cof a car? - the cost
bof a boat?
So, I will have a better idea about his fortune. Note that if m-n is big enough, you might have a lot of possible answers.
Each answer should be given as ["M: f", "B: b", "C: c"] and all the answers as [ ["M: f", "B: b", "C: c"], ... ]. "M" stands for money, "B" for boats, "C" for cars.
Note: m, n, f, b, c are positive integers, where 0 <= m <= n or m >= n >= 0. m and n are inclusive.
Examples:
howmuch(1, 100) => [["M: 37", "B: 5", "C: 4"], ["M: 100", "B: 14", "C: 11"]]
howmuch(1000, 1100) => [["M: 1045", "B: 149", "C: 116"]]
howmuch(10000, 9950) => [["M: 9991", "B: 1427", "C: 1110"]]
howmuch(0, 200) => [["M: 37", "B: 5", "C: 4"], ["M: 100", "B: 14", "C: 11"], ["M: 163", "B: 23", "C: 18"]]
Explanation of the results for howmuch(1, 100):
In the first answer his possible fortune is 37:
- so he can buy 7 boats each worth 5:
37 - 7 * 5 = 2 - or he can buy 9 cars worth 4 each:
37 - 9 * 4 = 1
- so he can buy 7 boats each worth 5:
- The second possible answer is 100:
- he can buy 7 boats each worth 14:
100 - 7 * 14 = 2 - or he can buy 9 cars worth 11:
100 - 9 * 11 = 1
- he can buy 7 boats each worth 14:
-------------------------------------------------------------------------------------------------------------------
题目大意为给出一个数的范围,在此范围中找出数字,满足公式(这个整数-7*某个整数)=2以及满足(这个整数-9*某个整数)=1,然后返回这个数以及符合条件的“某个整数”的值
解题办法:
看一下我的解题办法,重复代码好多,,,,
def howmuch(m, n):
# your code
L = []
if n >= m:
for i in range(m, n+1):
L1 = []
b = (i-2) % 7
c = (i-1) % 9
if b == 0 and c == 0:
L1 = ["M: "+str(i), "B: "+str(int((i-2)/7)), "C: "+str(int((i-1)/9))]
L.append(L1)
else:
for i in range(n, m+1):
L1 = []
b = (i-2) % 7
c = (i-1) % 9
if b == 0 and c == 0:
L1 = ["M: "+str(i), "B: "+str(int((i-2)/7)), "C: "+str(int((i-1)/9))]
L.append(L1) return L
再看看网友优秀的思路:
def howmuch(m, n):
return [['M: %d'%i, 'B: %d'%(i/7), 'C: %d'%(i/9)] for i in range(min(m,n), max(m,n)+1) if i%7 == 2 and i%9 == 1]
简洁到令人发指,,,羡慕啊!!!
知识点:
1、取两个数的最大值和最小值,可以使用min()和max()函数来取出最大值和最小值;也可以使用n, m = m, n if m>n:
2、i = 1; (["M: %s"])%i -------->这是一种错误的办法
【Kata Daily 190908】How Much?的更多相关文章
- 【Kata Daily 190929】Password Hashes(密码哈希)
题目: When you sign up for an account somewhere, some websites do not actually store your password in ...
- 【Kata Daily 191012】Find numbers which are divisible by given number
题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- 【Kata Daily 190927】Counting sheep...(数绵羊)
题目: Consider an array of sheep where some sheep may be missing from their place. We need a function ...
- 【Kata Daily 190924】Difference of Volumes of Cuboids(长方体的体积差)
题目: In this simple exercise, you will create a program that will take two lists of integers, a and b ...
- 【Kata Daily 190923】Odder Than the Rest(找出奇数)
题目: Create a method that takes an array/list as an input, and outputs the index at which the sole od ...
- 【Kata Daily 190920】Square(n) Sum(平方加总)
题目: Complete the square sum function so that it squares each number passed into it and then sums the ...
- 【Kata Daily 190919】Sort Out The Men From Boys(排序)
题目: Scenario Now that the competition gets tough it will Sort out the men from the boys . Men are th ...
- 【Kata Daily 190918】Spacify(插空)
题目: Modify the spacify function so that it returns the given string with spaces insertedbetween each ...
随机推荐
- 你知道CPU结构也会影响Redis性能吗?
啦啦啦,我是卖身不卖艺的二哈,ε=(´ο`*)))唉错啦(我是开车的二哈),我又来了,铁子们一起开车呀! 今天来分析下CPU结构对Redis性能会有影响吗? 在进行Redis性能分析的时候,通常我们会 ...
- 用vscode写python,from引用本地文件的时候老是有红色波浪线,很不爽
前言 出于一些原因,国际关系等等,最近想把开发工具切换到一些免费开源的工具上面,先尝试了在vscode上搭建python环境,总体还是很简单的, 网上教程很多,vscode本身的插件也很丰富,可惜了国 ...
- Python+Appium自动化测试(7)-截图方法
一,selenium模块的两种截图方法 get_screenshot_as_file(filename) 参数filename为截图文件保存的绝对路径,如: driver.get_screenshot ...
- 极简 Node.js 入门 - 5.2 url & querystring
极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...
- day27 Pyhton 面向对象02 组合
# 组合 # 什么是组合 : 一个类对象的属性是另外一个类的对象 class Person: def __init__(self,name,sex,hp,mp,ad): self.name = nam ...
- mysql 登陆与退出
Mysql登陆与退出 登陆参数 登陆命令 mysql -uroot -p 回车输入密码 退出有三个命令: exit quit \q 修改mysql提示符 连接mysql客户端时通过参数指定: 登 ...
- kafka-伪集群搭建
一.简介 Apache Kafka是一个快速.可扩展的.高吞吐的.可容错的分布式"发布-订阅"消息系统,使用Scala与Java语言编写,能够将消息从一个端点传递到另一个端点, ...
- linux安装jdk-centos7系统:
1 官网下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
- spring boot:实现图片文件上传并生成缩略图(spring boot 2.3.1)
一,为什么要给图片生成缩略图? 1, 用户上传的原始图片如果太大,不能直接展示在网站页面上, 因为不但流费server的流量,而且用户打开时非常费时间, 所以要生成缩略图. 2,服务端管理图片要注意的 ...
- docker的常用操作之三:网络配置
一, docker安装后容器使用哪些网络类型? 在宿主机执行如下命令: [root@localhost liuhongdi]# docker network ls NETWORK ID NAME DR ...