GoogleFoobarCode邀请挑战前篇

2023-11-11 06:30

本文主要是介绍GoogleFoobarCode邀请挑战前篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址

原文链接

前言

今天在谷歌的时候,突然弹出了游戏邀请,点进去会发现进入一个Linux系统,并且给了一些基本命令

Use the following shell commands:cd	change directory [dir_name]
cat	print file [file_name]
deleteme	delete all of your data associated with foobar
edit	open file in editor [file_name]
feedback	provide feedback on foobar
less	print a file a page at a time [file_name]
ls	list directory contents [dir_name]
request	request new challenge
status	print progress
submit	submit final solution file for assessment [file_name]
verify	runs tests on solution file [file_name]Keyboard help:Ctrl + S	save the open file [when editor is focused]
Ctrl + E	close the editor [when editor is focused]Toggle between the editor and terminal using ESC followed by TAB, then activate with ENTER.

以及一个小故事

Success! You've managed to infiltrate Commander Lambda's evil organization, and finally earned yourself an entry-level position as a Minion on their space station. From here, you just might be able to subvert Commander Lambda's plans to use the LAMBCHOP doomsday device to destroy Bunny Planet. Problem is, Minions are the lowest of the low in the Lambda hierarchy. Better buck up and get working, or you'll never make it to the top...

起初我以为是什么智力小游戏,在系统里面解密什么的结果使用request后,还是码代码

在这里插入图片描述

附上网站,不过需要邀请就是了,可能是在某个时间段触发某个搜索关键词可以收到邀请

解决方案

re-id

第一题是

Re-ID
=====There's some unrest in the minion ranks: minions with ID numbers like "1", "42", and other "good" numbers have been lording it over the poor minions who are stuck with more boring IDs. To quell the unrest, Commander Lambda has tasked you with reassigning everyone new random IDs based on a Completely Foolproof Scheme. Commander Lambda has concatenated the prime numbers in a single long string: "2357111317192329...". Now every minion must draw a number from a hat. That number is the starting index in that string of primes, and the minion's new ID number will be the next five digits in the string. So if a minion draws "3", their ID number will be "71113". Help the Commander assign these IDs by writing a function solution(n) which takes in the starting index n of Lambda's string of all primes, and returns the next five digits in the string. Commander Lambda has a lot of minions, so the value of n will always be between 0 and 10000.Languages
=========To provide a Java solution, edit Solution.java
To provide a Python solution, edit solution.pyTest cases
==========
Your code should pass the following test cases.
Note that it may also be run against hidden test cases not shown here.-- Java cases --
Input:
Solution.solution(0)
Output:23571Input:
Solution.solution(3)
Output:71113-- Python cases --
Input:
solution.solution(0)
Output:23571Input:
solution.solution(3)
Output:71113Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.

解决方案

class KotomiApplicationTests {    public static String solution(int n) {if (0 == n) {return "23571";}//build prime arrint primaryCount = 3 * n + 20;boolean[] prime = new boolean[primaryCount];//initprime[2] = true;for (int count = 1; count < primaryCount; count = count + 2) {prime[count] = true;}//Sieve of Eratosthenesint maxCount = (int) Math.sqrt(primaryCount);int count = 3;while (count <= maxCount) {for (int result = count * count; result < primaryCount; result += 2 * count) {if (prime[result]) {prime[result] = false;}}do {count += 2;} while (count <= maxCount && !prime[count]);}//readArrayDeque<Integer> primDeque = new ArrayDeque<>();primDeque.add(2);primDeque.add(3);primDeque.add(5);primDeque.add(7);primDeque.add(11);int length = 0;int curNum = 12;int lastNum = 0;while (length < n) {if (prime[curNum]) {primDeque.addLast(curNum);lastNum = primDeque.removeFirst();length += String.valueOf(lastNum).length();}++curNum;}StringBuilder str = new StringBuilder().append(lastNum);while (!primDeque.isEmpty()) {str.append(primDeque.removeFirst());}int startIndex = String.valueOf(lastNum).length() - length + n;return str.substring(startIndex, startIndex + 5);}
}

逻辑上比较简单,将素数标记,然后使用滑动窗口即可,代码应该还有可以优化的部分,如果小伙伴们有更好的解决方案可以在评论区提供

elevator-maintenance

Elevator Maintenance
====================You've been assigned the onerous task of elevator maintenance -- ugh! It wouldn't be so bad, except that all the elevator documentation has been lying in a disorganized pile at the bottom of a filing cabinet for years, and you don't even know what elevator version numbers you'll be working on. Elevator versions are represented by a series of numbers, divided up into major, minor and revision integers. New versions of an elevator increase the major number, e.g. 1, 2, 3, and so on. When new features are added to an elevator without being a complete new version, a second number named "minor" can be used to represent those new additions, e.g. 1.0, 1.1, 1.2, etc. Small fixes or maintenance work can be represented by a third number named "revision", e.g. 1.1.1, 1.1.2, 1.2.0, and so on. The number zero can be used as a major for pre-release versions of elevators, e.g. 0.1, 0.5, 0.9.2, etc (Commander Lambda is careful to always beta test her new technology, with her loyal henchmen as subjects!).Given a list of elevator versions represented as strings, write a function solution(l) that returns the same list sorted in ascending order by major, minor, and revision number so that you can identify the current elevator version. The versions in list l will always contain major numbers, but minor and revision numbers are optional. If the version contains a revision number, then it will also have a minor number.For example, given the list l as ["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"], the function solution(l) would return the list ["1.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]. If two or more versions are equivalent but one version contains more numbers than the others, then these versions must be sorted ascending based on how many numbers they have, e.g ["1", "1.0", "1.0.0"]. The number of elements in the list l will be at least 1 and will not exceed 100.Languages
=========To provide a Python solution, edit solution.py
To provide a Java solution, edit Solution.javaTest cases
==========
Your code should pass the following test cases.
Note that it may also be run against hidden test cases not shown here.-- Python cases --
Input:
solution.solution(["1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"])
Output:0.1,1.1.1,1.2,1.2.1,1.11,2,2.0,2.0.0Input:
solution.solution(["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"])
Output:1.0,1.0.2,1.0.12,1.1.2,1.3.3-- Java cases --
Input:
Solution.solution({"1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"})
Output:0.1,1.1.1,1.2,1.2.1,1.11,2,2.0,2.0.0Input:
Solution.solution({"1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"})
Output:1.0,1.0.2,1.0.12,1.1.2,1.3.3Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.

这题似乎比上题更简单,直接使用一个小顶堆,然后自定义排序方法即可

class KotomiApplicationTests {
//jdk8public static Comparator<String> comparator = (str1, str2) -> {List<Integer> str1Arr = new java.util.ArrayList<>(Arrays.stream(str1.split("\\.")).map(Integer::valueOf).toList());List<Integer> str2Arr = new java.util.ArrayList<>(Arrays.stream(str2.split("\\.")).map(Integer::valueOf).toList());while (str1Arr.size() < 3) {str1Arr.add(-1);}while (str2Arr.size() < 3) {str2Arr.add(-1);}for (int count = 0; count < 3; ++count) {int res = str1Arr.get(count) - str2Arr.get(count);if (0 != res) {return res;}}return 0;};public static String[] solution(String[] l) {String[] ans = new String[l.length];PriorityQueue<String> leap = new PriorityQueue<>(comparator);leap.addAll(Arrays.asList(l));int count = 0;while (!leap.isEmpty()) {ans[count] = leap.poll();++count;}return ans;}
}

还有jdk17的实现,由于我是使用的17就顺便贴上了

class KotomiApplicationTests {
//jdk17public static Comparator<String> comparator = (str1, str2) -> {List<Integer> str1Arr = new java.util.ArrayList<>(Arrays.stream(str1.split("\\.")).map(Integer::valueOf).toList());List<Integer> str2Arr = new java.util.ArrayList<>(Arrays.stream(str2.split("\\.")).map(Integer::valueOf).toList());while (str1Arr.size() < 3) {str1Arr.add(-1);}while (str2Arr.size() < 3) {str2Arr.add(-1);}for (int count = 0; count < 3; ++count) {int res = str1Arr.get(count) - str2Arr.get(count);if (0 != res) {return res;}}return 0;};public static String[] solution(String[] l) {String[] ans = new String[l.length];PriorityQueue<String> leap = new PriorityQueue<>(comparator);leap.addAll(Arrays.asList(l));int count = 0;while (!leap.isEmpty()) {ans[count] = leap.poll();++count;}return ans;}
}

hey-i-already-did-that

Hey, I Already Did That!
========================Commander Lambda uses an automated algorithm to assign minions randomly to tasks, in order to keep minions on their toes. But you've noticed a flaw in the algorithm -- it eventually loops back on itself, so that instead of assigning new minions as it iterates, it gets stuck in a cycle of values so that the same minions end up doing the same tasks over and over again. You think proving this to Commander Lambda will help you make a case for your next promotion. You have worked out that the algorithm has the following process: 1) Start with a random minion ID n, which is a nonnegative integer of length k in base b
2) Define x and y as integers of length k.  x has the digits of n in descending order, and y has the digits of n in ascending order
3) Define z = x - y.  Add leading zeros to z to maintain length k if necessary
4) Assign n = z to get the next minion ID, and go back to step 2For example, given minion ID n = 1211, k = 4, b = 10, then x = 2111, y = 1112 and z = 2111 - 1112 = 0999. Then the next minion ID will be n = 0999 and the algorithm iterates again: x = 9990, y = 0999 and z = 9990 - 0999 = 8991, and so on.Depending on the values of n, k (derived from n), and b, at some point the algorithm reaches a cycle, such as by reaching a constant value. For example, starting with n = 210022, k = 6, b = 3, the algorithm will reach the cycle of values [210111, 122221, 102212] and it will stay in this cycle no matter how many times it continues iterating. Starting with n = 1211, the routine will reach the integer 6174, and since 7641 - 1467 is 6174, it will stay as that value no matter how many times it iterates.Given a minion ID as a string n representing a nonnegative integer of length k in base b, where 2 <= k <= 9 and 2 <= b <= 10, write a function solution(n, b) which returns the length of the ending cycle of the algorithm above starting with n. For instance, in the example above, solution(210022, 3) would return 3, since iterating on 102212 would return to 210111 when done in base 3. If the algorithm reaches a constant, such as 0, then the length is 1.Languages
=========To provide a Java solution, edit Solution.java
To provide a Python solution, edit solution.pyTest cases
==========
Your code should pass the following test cases.
Note that it may also be run against hidden test cases not shown here.-- Java cases --
Input:
Solution.solution('210022', 3)
Output:3Input:
Solution.solution('1211', 10)
Output:1-- Python cases --
Input:
solution.solution('1211', 10)
Output:1Input:
solution.solution('210022', 3)
Output:3Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.

同样比较简单,双指针判圈

class KotomiApplicationTests {    static String iteratorMethod(String num, int b) {String small = num.chars().sorted().collect(StringBuffer::new, StringBuffer::appendCodePoint, StringBuffer::append).toString();String big = num.chars().sorted().collect(StringBuffer::new, StringBuffer::appendCodePoint, StringBuffer::append).reverse().toString();StringBuilder target = new StringBuilder(Integer.toString(Integer.parseInt(big, b) - Integer.parseInt(small, b), b));while (target.length() < num.length()) {target.insert(0, "0");}return target.toString();}public static int solution(String n, int b) {String slowPtr = iteratorMethod(n, b);String fastPtr = iteratorMethod(iteratorMethod(n, b), b);//floydwhile (!slowPtr.equals(fastPtr)) {slowPtr = iteratorMethod(slowPtr, b);fastPtr = iteratorMethod(iteratorMethod(fastPtr, b), b);}fastPtr = n;while (!slowPtr.equals(fastPtr)) {slowPtr = iteratorMethod(slowPtr, b);fastPtr = iteratorMethod(fastPtr, b);}String startPtr = fastPtr;//lengthint length = 1;slowPtr = iteratorMethod(slowPtr, b);while (!slowPtr.equals(startPtr)) {slowPtr = iteratorMethod(slowPtr, b);++length;}return length;}
}

这样前两关就基本完事了,之后的题目得空再玩吧

Level 2 complete
You are now on level 3
Challenges left to complete level: 3Level 1: 100% [==========================================]
Level 2: 100% [==========================================]
Level 3:   0% [..........................................]
Level 4:   0% [..........................................]
Level 5:   0% [..........................................]

原文地址

原文链接

这篇关于GoogleFoobarCode邀请挑战前篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/388154

相关文章

跨国公司撤出在华研发中心的启示:中国IT产业的挑战与机遇

近日,IBM中国宣布撤出在华的两大研发中心,这一决定在IT行业引发了广泛的讨论和关注。跨国公司在华研发中心的撤出,不仅对众多IT从业者的职业发展带来了直接的冲击,也引发了人们对全球化背景下中国IT产业竞争力和未来发展方向的深思。面对这一突如其来的变化,我们应如何看待跨国公司的决策?中国IT人才又该如何应对?中国IT产业将何去何从?本文将围绕这些问题展开探讨。 跨国公司撤出的背景与

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

国产游戏行业的崛起与挑战:技术创新引领未来

国产游戏行业的崛起与挑战:技术创新引领未来 近年来,国产游戏行业蓬勃发展,技术水平不断提升,许多优秀作品在国际市场上崭露头角。从画面渲染到物理引擎,从AI技术到服务器架构,国产游戏已实现质的飞跃。然而,面对全球游戏市场的激烈竞争,国产游戏技术仍然面临诸多挑战。本文将探讨这些挑战,并展望未来的机遇,深入分析IT技术的创新将如何推动行业发展。 国产游戏技术现状 国产游戏在画面渲染、物理引擎、AI

参会邀请 | 第二届机器视觉、图像处理与影像技术国际会议(MVIPIT 2024)

第二届机器视觉、图像处理与影像技术国际会议(MVIPIT 2024)将于2024年9月13日-15日在中国张家口召开。 MVIPIT 2024聚焦机器视觉、图像处理与影像技术,旨在为专家、学者和研究人员提供一个国际平台,分享研究成果,讨论问题和挑战,探索前沿技术。诚邀高校、科研院所、企业等有关方面的专家学者参加会议。 9月13日(周五):签到日 9月14日(周六):会议日 9月15日(周日

分库分表:应对大数据量挑战的数据库扩展策略

随着互联网技术的发展,数据量的爆炸性增长给数据库系统带来了前所未有的挑战。为了有效管理大规模数据并保持高性能,分库分表成为了一种常见的数据库扩展策略。本文将探讨分库分表的概念、动机、实施策略以及潜在的挑战和解决方案。 什么是分库分表? 分库分表是一种数据库架构设计策略,它将数据分散存储在多个数据库(分库)和多个表(分表)中。这种方法可以提高数据库的可伸缩性、可用性和性能。 为什么需要分库分表

《深入理解 C++模板分离编译:挑战与解决方案》

在 C++编程的广阔领域中,模板是一个强大而复杂的特性,它为程序员提供了高度的灵活性和代码复用性。然而,模板的分离编译却常常成为开发者们面临的一个难题。本文将深入探讨 C++中模板的分离编译问题,揭示其背后的原理、挑战以及解决方案。 一、模板的强大之处 C++模板允许程序员编写通用的代码,可以适应不同的数据类型和场景。通过模板,我们可以实现泛型编程,提高代码的可维护性和可扩展性。例如,我们可以

小琳AI课堂:大模型的发展历程:从创新到挑战

大家好,这里是小琳AI课堂。今天我们来聊聊大模型的发展历程,这是一段充满创新与挑战的故事。🚀 让我们追溯到20世纪50年代,那时计算机科学和人工智能还处于萌芽阶段。 早期探索(1950s-1970s) 早期探索是大模型发展历程中的一个关键时期,它标志着人工智能作为一门学科的诞生。这个时期,计算机科学和人工智能还处于起步阶段,科学家们充满好奇地探索如何让机器模仿人类智能。这一阶段的探索不仅奠定

美联储降息在即:加密市场风云再起,机遇与挑战并存

随着全球金融市场日益与加密货币市场的波动紧密相连,美联储的货币政策调整正成为加密市场的重要风向标。今年以来,比特币等主流加密货币反复跟随全球市场震荡,宏观经济指标对加密市场的影响愈加显著。尤其是美国联邦基金利率的变动,已成为整个加密产业的核心关注点。 美联储加息的影响回顾 自2022年3月到2023年7月,美联储连续11次加息,累计幅度达到525个基点,创下了近半个世纪以来美联储加息的

《挑战极限,畅享精彩 ——韩星地带:逃脱任务 3 震撼来袭》

在综艺的浩瀚星海中,总有那么一些节目如璀璨星辰般闪耀,而《韩星地带:逃脱任务 3》无疑就是其中的佼佼者。 2024 年,这个令人热血沸腾的真人秀节目再度回归,为观众带来一场惊心动魄的冒险之旅。节目由韩国 “国民 MC” 刘在石领衔主持,他那无与伦比的综艺感和控场能力,如同定海神针般,稳稳地把控着节目的节奏。权俞利,少女时代的魅力成员,勇敢与智慧并存,在节目中展现出令人惊叹的一面。新加入的金东炫

备战秋招60天算法挑战,Day32

题目链接: https://leetcode.cn/problems/house-robber-ii/ 视频题解: https://www.bilibili.com/video/BV1WRYKeKEQE/ LeetCode 213. 打家劫舍 II 题目描述 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 ,这意味着第一个房屋和最后一个