本文主要是介绍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邀请挑战前篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!