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

Guess Number

You have access to an interactive server that hosts a secret puzzle. Your objective is to guess a hidden 4-digit number. The secret number is composed of digits '0' through '9', and may have leading zeros.

To solve the puzzle, you interact with a predefined server named GuessServer, which exposes a function guessNumber. You submit a 4-digit string to this function as your guess, and it returns the number of digits that are correct in both value and position.

The definition of GuessServer is as follows:

class GuessServer {
    ...
    public int guessNumber(String guess) {...}
}
 

For example, if the secret is "1211":

  • guessNumber("1214") returns 3, because the digits "1", "2", and "1" are all correct and in the proper positions of the secret.
  • guessNumber("9999") returns 0.

Your task is to implement a function that determines the secret number by making multiple calls to GuessServer, and find the secret with as few guesses as possible.

Constraints:

  • The secret number is always a 4-digit string.
  • Each digit is a character from '0' to '9'.

Example

Input: secret = "1234" (unknown to your function) Output: "1234" Explanation: Your function should output the exact secret after as few calls as possible.

  1 class GuessServer:
  2     def __init__(self, secret):
  3         self.secret = secret
  4         self.callCount = 0
  5 
  6     def guessNumber(self, guess):
  7         self.callCount += 1
  8         matches = 0
  9         for i in range(4):
 10             if guess[i] == self.secret[i]:
 11                 matches += 1
 12         return matches
 13 
 14     def getCallCount(self):
 15         return self.callCount
 16 
 17 
 18 class Solution:
 19     def __init__(self, server):
 20         self.server = server
 21 
 22     def guessSecret(self):
 23         # We start with a baseline guess list
 24         current_guess = ['0', '0', '0', '0']
 25         
 26         # Get the baseline score for "0000"
 27         baseline_score = self.server.guessNumber("".join(current_guess))
 28         
 29         # If "0000" is already perfect, we are done in 1 call!
 30         if baseline_score == 4:
 31             return "".join(current_guess)
 32             
 33         # Iterate through each of the 4 positions
 34         for position in range(4):
 35             # Try changing the digit at the current position from '1' to '9'
 36             for digit in range(1, 10):
 37                 current_guess[position] = str(digit)
 38                 new_score = self.server.guessNumber("".join(current_guess))
 39                 
 40                 # If the score increases, this digit is correct for this position!
 41                 if new_score > baseline_score:
 42                     baseline_score = new_score
 43                     break
 44                 # If the score decreases, it means '0' was actually the correct digit!
 45                 elif new_score < baseline_score:
 46                     current_guess[position] = '0'
 47                     break
 48                 # If the score stays the same, '0' wasn't correct, and neither is this digit.
 49                 # Loop continues to test the next digit.
 50                 
 51         return "".join(current_guess)
 52 
 53 
 54 def test1():
 55     print("===== Test 1 =====")
 56     guessServer = GuessServer("1234")
 57     solution = Solution(guessServer)
 58     result = solution.guessSecret()
 59     print("Guessed number: " + result)  # Expected: 1234
 60     print("Total calls: " + str(guessServer.getCallCount()))
 61     print()
 62 
 63 
 64 def test2():
 65     print("===== Test 2 =====")
 66     guessServer = GuessServer("0809")
 67     solution = Solution(guessServer)
 68     result = solution.guessSecret()
 69     print("Guessed number: " + result)  # Expected: 0809
 70     print("Total calls: " + str(guessServer.getCallCount()))
 71     print()
 72 
 73 
 74 def test3():
 75     print("===== Test 3 =====")
 76     guessServer = GuessServer("5578")
 77     solution = Solution(guessServer)
 78     result = solution.guessSecret()
 79     print("Guessed number: " + result)  # Expected: 5578
 80     print("Total calls: " + str(guessServer.getCallCount()))
 81     print()
 82 
 83 
 84 def test4():
 85     print("===== Test 4 =====")
 86     guessServer = GuessServer("0000")
 87     solution = Solution(guessServer)
 88     result = solution.guessSecret()
 89     print("Guessed number: " + result)  # Expected: 0000
 90     print("Total calls: " + str(guessServer.getCallCount()))
 91     print()
 92 
 93 
 94 def test5():
 95     print("===== Test 5 =====")
 96     guessServer = GuessServer("9999")
 97     solution = Solution(guessServer)
 98     result = solution.guessSecret()
 99     print("Guessed number: " + result)  # Expected: 9999
100     print("Total calls: " + str(guessServer.getCallCount()))
101     print()
102 
103 
104 if __name__ == "__main__":
105     test1()
106     test2()
107     test3()
108     test4()
109     test5()

 

http://www.jsqmd.com/news/832253/

相关文章:

  • 前端文档工程化实践:基于MkDocs与GitHub Actions的自动化文档体系
  • 终极MifareOneTool完全指南:零基础掌握Windows最强NFC卡片管理工具
  • Go语言构建高性能广告数据处理管道:goads-green架构与实战
  • 知识竞赛的“锦囊”设计:场外求助、免答权、双倍分
  • 危化园区:三维重构+透明建筑 -实时查询
  • 揭秘铁银印相×Midjourney融合逻辑:从胶片化学反应到AI潜影映射的5步精准转译流程
  • 文档智能实战:基于MaClaw的端到端信息抽取流水线构建指南
  • 2026年黑龙江防渗复合膜市场:如何甄选适配的供应商与服务 - 2026年企业推荐榜
  • 基于ATTiny85与DotStar LED的POV流光球制作全解析
  • 会话管理封装实践:构建安全可扩展的分布式会话系统
  • 【限时技术解禁】ElevenLabs未公开的泰米尔文SSML扩展语法(含重音标记、数词朗读规则、敬语语调控制),仅剩72小时可查
  • 安卓客户端架构解析:从MVVM到网络通信的完整实践
  • SiPM与LYSO闪烁光纤在质子治疗监测中的关键技术解析
  • 第3讲:栈(Stack)
  • 汽车该多久换一代
  • WinDirStat:Windows磁盘空间管理神器,让存储问题无处遁形
  • 2026年评价高的包头砂浆/包头混凝土砂浆品牌厂家推荐 - 品牌宣传支持者
  • ElevenLabs僧伽罗文输出不自然?不是模型问题——而是你漏掉了这4个语言学预处理层(附Python自动化清洗脚本)
  • Stream-Omni:流式文本处理与全局上下文融合的NLP新架构
  • 深度解析VS Code Live Server:高效前端开发实时预览配置秘籍
  • 智慧城市:数字孪生+三维重构 -透明建筑
  • 用鼠标模拟触摸事件:前端开发与测试的Web交互模拟方案
  • 从科幻到现实:用PCB艺术与电容触摸芯片打造交互式LCARS面板
  • API响应延迟高达1.8s?ElevenLabs英文语音生成性能瓶颈诊断与毫秒级优化方案,限内网测试数据首发
  • ElevenLabs韩文语音延迟优化至387ms:WebSocket流式传输+边缘缓存双引擎实战配置(附压测数据)
  • API文档协作中心构建指南:从工程化实践到团队效能提升
  • 5步轻松解锁B站缓存视频:m4s-converter完整使用指南
  • 【菲律宾市场语音本地化权威报告】:基于172小时真实用户反馈,ElevenLabs菲语合成自然度达4.68/5.0——但3类场景仍需人工校准
  • 2026年评价高的轻量化复合材料光伏支架横向对比厂家推荐 - 品牌宣传支持者
  • AI 能不能教孩子提问