guess

LeetCode_374. Guess Number Higher or Lower

给你一囗甜甜゛ 提交于 2019-12-04 09:45:00
374. Guess Number Higher or Lower Easy We are playing the Guess Game. The game is as follows: I pick a number from 1 to n . You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number is higher or lower. You call a pre-defined API guess(int num) which returns 3 possible results ( -1 , 1 , or 0 ): -1 : My number is lower 1 : My number is higher 0 : Congrats! You got it! Example : Input: n = 10, pick = 6 Output: 6 package leetcode.easy; /* The guess API is defined in the parent class GuessGame. @param num, your guess @return -1 if my number is lower, 1

299. Bulls and Cows

删除回忆录丶 提交于 2019-12-03 16:19:34
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number. Write a function to return a hint according to the secret number and friend's guess, use A to

猜数字游戏

蹲街弑〆低调 提交于 2019-12-03 14:19:01
猜数字游戏,用到了生成随机数 1 package main 2 3 import ( 4 "fmt" 5 "math/rand" 6 "strconv" 7 "time" 8 ) 9 10 func main() { 11 //生成一个随机数 12 myRand := rand.New(rand.NewSource(time.Now().UnixNano())) 13 answer := myRand.Intn(1000) 14 15 fmt.Println("按Q可退出") 16 17 for { 18 19 //接收用户的输入 20 fmt.Println("请输入你的猜想: ") 21 22 var guess string 23 fmt.Scan(&guess) 24 25 //用户想要提前退出 26 if guess == "q" || guess == "Q" { 27 break 28 } 29 30 //用户的输入转换成整数 31 guessNum, _ := strconv.Atoi(guess) 32 33 //反馈猜测的结果 34 switch { 35 case guessNum > answer: 36 fmt.Println(guessNum,"太大了....") 37 case guessNum < answer: 38 fmt.Println

leetcode刷题-1

旧城冷巷雨未停 提交于 2019-12-03 11:56:17
小A 和 小B 在玩猜数字。小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜。他们一共进行三次这个游戏,请返回 小A 猜对了几次? 输入的guess数组为 小A 每次的猜测,answer数组为 小B 每次的选择。guess和answer的长度都等于3。 示例 1: 输入:guess = [1,2,3], answer = [1,2,3] 输出:3 解释:小A 每次都猜对了。 示例 2: 输入:guess = [2,2,3], answer = [3,2,1] 输出:1 解释:小A 只猜对了第二次。 限制: guess的长度 = 3 answer的长度 = 3 guess的元素取值为 {1, 2, 3} 之一。 answer的元素取值为 {1, 2, 3} 之一。 来源:力扣(LeetCode) 链接: https://leetcode-cn.com/problems/guess-numbers 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 代码: public int uniqueMorseRepresentations(String[] words) { String[] dict = new String[]{".-","-...","-.-.","-..",".","..-.","--.","....","..

Python- Stuck on a guessing game&#039;s program?

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: So, I've been working on this guessing game problem for a while and I am left scratching my brain for the past 2 hours trying to figure out what's wrong but I can't. I also tried searching for the solution but I don't wanna do copy & paste and I actually want to solve my code by myself. Here's what I've been able to get so far: start = 0 end = 100 print 'Please think of a number between 0 and 100!' user = '' ans = (start + end) / 2 while user != 'c': print ('Is your secret number ' + str((start + end) / 2) + '?') user = raw_input("Enter 'h'

python基础一

强颜欢笑 提交于 2019-12-03 06:45:56
1.布尔值(True,False) 布尔类型很简单,就两个值 ,一个True(真),一个False(假), 主要用记逻辑判断 >>> a=3 >>> b=5 >>> >>> a > b #不成立就是False,即假 False >>> >>> a < b #成立就是True, 即真 True 2.程序交互input # 将用户输入的内容赋值给 name 变量 name = input("请输入用户名:") # 打印输入的内容 print(name) 执行脚本就会发现,程序会等待你输入姓名后再往下继续走。 可以让用户输入多个信息,如下 #!/usr/bin/env python # -*- coding: utf-8 -*- name = input("What is your name?") age = input("How old are you?") hometown = input("Where is your hometown?") print("Hello ",name , "your are ", age , "years old, you came from",hometown) 3.流程控制—if、elif、else if 条件: 满足条件执行代码 elif 条件: 上面的条件不满足就走这个 elif 条件: 上面的条件不满足就走这个 elif 条件:

Tried to guess R&#039;s HOME but no R command in the PATH. OsX 10.6

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to install rpy2 and I am facing a common issue. Unfortunately all the solution I have found are for win7 I have installed a Python 2.7 and R 2.15. then I write on the terminal easy_install rpy2 or, alternatively pip install rpy2 Same result: Tried to guess R's HOME but no R command in the PATH What I should do? 回答1: The rpy2 code is doing the wrong check. R might be perfectly fine, but rpy2 is using an unreliable check. To check for R , the rpy2 uses subprocess.check_output . However, that was introduced (AFAIK) in python 2.7. If

Tried to guess R's HOME but no R command in the PATH. OsX 10.6

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am trying to install rpy2 and I am facing a common issue. Unfortunately all the solution I have found are for win7 I have installed a Python 2.7 and R 2.15. then I write on the terminal easy_install rpy2 or, alternatively pip install rpy2 Same result: Tried to guess R 's HOME but no R command in the PATH What I should do? 回答1: The rpy2 code is doing the wrong check. R might be perfectly fine, but rpy2 is using an unreliable check. To check for R , the rpy2 uses subprocess.check_output . However, that was introduced (AFAIK) in

Guess timezone name for custom date

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I know that I can get browser timezone name: moment.tz.guess() How can I guess timezone for custom date? e.g. moment("2020-12-30T14:17:40+11:00").tz.guess() ? 回答1: You can't. There are 9 different location-based zones that use +11:00 year round, and 5 more that use it for daylight saving time. You might be able to eliminate a few if the date and time put it into a time zone where daylight time is not in effect and the standard time doesn't match, but from there, the best you could do would be to pick one at random. See "time zone !

[leetcode]374. Guess Number Higher or Lower

匿名 (未验证) 提交于 2019-12-03 00:30:01
[leetcode]374. Guess Number Higher or Lower Analysis 雨很大~―― [ummm~] We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I’ll tell you whether the number is higher or lower. 类似于猜价格的题,二分查找解决。 Implement // Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 int guess(int num); class Solution { public : int guessNumber(int n) { if (guess(n) == 0 ) return n; int lo = 1 ; int hi = n; int mid; while (lo < hi){