Add new stuff

Current status: level 3, 67%
This commit is contained in:
hedy 2022-09-03 11:24:02 +08:00
parent 887b5fcb54
commit eb02854ebf
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
14 changed files with 430 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
__pycache__
*.class
Solution

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 hedy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,16 @@
### pre reqs
* java (8+)
* python (2+)
Compile java:
```sh
javac -classpath .:target/dependency/* -d . Solution.java
```
Run:
```sh
java -classpath .:target/dependency/* Solution 0 3
```

View File

@ -0,0 +1,19 @@
class Solution {
public static int solution(int start, int length) {
int ans = start;
for (int n=1; n<=length; n++) {
int items = -n + length + 1;
int first = (n-1) * length + start;
for (int x=first; x<items+first; x++) {
ans = ans^x;
}
}
return ans^start;
}
public static void main(String[] args) {
System.out.println(Solution.solution(Integer.valueOf(args[0]), Integer.valueOf(args[1])));
}
}

View File

@ -0,0 +1,82 @@
# Queue To Do
Time to solve: 168 hours.
## Description
You're almost ready to make your move to destroy the LAMBCHOP doomsday device, but the security checkpoints that guard the underlying systems of the LAMBCHOP are going to be a problem. You were able to take one down without tripping any alarms, which is great! Except that as Commander Lambda's assistant, you've learned that the checkpoints are about to come under automated review, which means that your sabotage will be discovered and your cover blown - unless you can trick the automated review system.
To trick the system, you'll need to write a program to return the same security checksum that the guards would have after they would have checked all the workers through. Fortunately, Commander Lambda's desire for efficiency won't allow for hours-long lines, so the checkpoint guards have found ways to quicken the pass-through rate. Instead of checking each and every worker coming through, the guards instead go over everyone in line while noting their security IDs, then allow the line to fill back up. Once they've done that they go over the line again, this time leaving off the last worker. They continue doing this, leaving off one more worker from the line each time but recording the security IDs of those they do check, until they skip the entire line, at which point they XOR the IDs of all the workers they noted into a checksum and then take off for lunch. Fortunately, the workers' orderly nature causes them to always line up in numerical order without any gaps.
For example, if the first worker in line has ID 0 and the security checkpoint line holds three workers, the process would look like this:
```
0 1 2 /
3 4 / 5
6 / 7 8
```
where the guards' XOR (^) checksum is 0^1^2^3^4^6 == 2.
Likewise, if the first worker has ID 17 and the checkpoint holds four workers, the process would look like:
```
17 18 19 20 /
21 22 23 / 24
25 26 / 27 28
29 / 30 31 32
```
which produces the checksum 17^18^19^20^21^22^23^25^26^29 == 14.
All worker IDs (including the first worker) are between 0 and 2000000000 inclusive, and the checkpoint line will always be at least 1 worker long.
With this information, write a function answer(start, length) that will cover for the missing security checkpoint by outputting the same checksum the guards would normally submit before lunch. You have just enough time to find out the ID of the first worker to be checked (start) and the length of the line (length) before the automatic review occurs, so your program must generate the proper checksum with just those two values.
## Languages
To provide a Python solution, edit solution.py
To provide a Java solution, edit solution.java
## Test Cases
### Test Case 1
Inputs:
```
(int) start = 0
(int) length = 3
```
Output:
```
(int) 2
```
### Test Case 2
Inputs:
```
(int) start = 17
(int) length = 4
```
Output:
```
(int) 14
```
## Constraints
### Java
Your code will be compiled using standard Java 7. It must implement the answer() method in the solution stub.
Execution time is limited. Some classes are restricted (e.g. java.lang.ClassLoader). You will see a notice if you use a restricted class when you verify your solution.
Third-party libraries, input/output operations, spawning threads or processes and changes to the execution environment are not allowed.
### Python
Your code will run inside a Python 2.7.6 sandbox.
Standard libraries are supported except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib.

View File

@ -0,0 +1,15 @@
import sys
def solution(start, length):
ans = start
for n in range(1, length+1):
items = -n + length + 1
first = (n-1) * length + start
for x in range(first, items+first):
ans = ans^x
return ans^start
# print(sys.argv)
print(solution(int(sys.argv[1]), int(sys.argv[2])))

22
level3/queue-to-do/test.sh Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
# build
rm *.class
javac -classpath .:target/dependency/* -d . Solution.java
java='java -classpath .:target/dependency/* Solution'
python='python3 solution.py'
for i in {1..1000}; do
s=$((1 + $RANDOM % 1000))
l=$((1 + $RANDOM % 1000))
a_j=$($java $s $l)
a_p=$($python $s $l)
if [[ "$a1" != "$a2" ]]; then
echo "s=$s" "l=$l" "j=$a_j" "p=$a_p"
else
echo -n .
fi
done
echo

View File

@ -0,0 +1,43 @@
// grandest staircase indeed... this was such an interesting puzzle!!!
// thought process: instead of going down the stairs... HOW ABOUT WE GO UP?!?!?!
// ;P
// build the stairs from bottom to top
//
// first implemented in python. after 6 iterations, it finally worked, but for larger numbers of n it took too long to evaluate (n=199 took 4 minutes)
// so then I converted it to java, and 199/200 both takes less than a minute!
class Solution {
public static void main(String[] args) {
System.out.println(solution(200));
}
public static int solution(int n) {
int[] result = s(n, 1, 1, 0);
return result[0];
}
public static int[] s(int n, int t, int start, int w) {
int[] ret = new int[2];
if (start >= n || n-t <= 0 || n-t <= start) {
t = t - start;
ret[0] = w;
ret[1] = start;
return ret;
}
w++;
int newstart = start + 1;
t = t + newstart;
int[] result = s(n, t, newstart, w);
w = result[0];
int oldstart = result[1];
if (oldstart != 0) {
t = t - start;
start++;
return s(n, t, start, w);
}
return ret;
}
}

View File

@ -0,0 +1,60 @@
import sys
def solution(n):
return s(0, n, 0)
def make(n, left):
print("make", n, left)
if left == 0:
return True
if left == 1 and n > 1:
return True
if left == 2 and n > 2:
return True
if n == 2 == left:
return False
if n == 3 == left:
# 3, 2, 1
return None
if n <= 3 and left >= 3:
return False
# FIXME: too many special cases
if n == 3 and left == 4:
return False
print("split", n, left)
return None # Can split more
def s(outer, n, ways):
if n == 3:
return 1 + ways
for i in reversed(range(n//2-1, n)):
if outer != 0 and outer <= i:
print("outer", outer)
continue
m = make(i, n-i)
if m:
ways += 1
print(i)
elif m is None:
if i > n-i:
ways += 1
print(i)
print("recursion", n, i, n-i)
ways = s(i, n-i, ways)
elif not m:
continue
return ways
if __name__ == '__main__':
print(solution(int(sys.argv[1])))

View File

@ -0,0 +1,46 @@
import sys
def solution(n):
return ss(n, 0, [0], 0)
def ss(n, t, s, w):
print("ss", n, t, s, w)
if n == 0 and len(s) == 1:
return w
if n < 0:
return w
last = s[-1]
if len(s) == 1 and s[0] == 0:
s.pop()
if n == 0:
w += 1
print(s)
last = s.pop()
t -= last
n += last
last = s.pop()
t -= last
n += last
elif t > n and n <= last:
last = s.pop()
t -= last
n += last
newlast = last+1
if newlast > (n+t)-1:
return w
s.append(newlast)
w = ss(n-(newlast), t+(newlast), s, w)
return w
if __name__ == '__main__':
print(solution(int(sys.argv[1])))

View File

@ -0,0 +1 @@
Completed in: 2 days, 1 hr, 23 mins, 3 secs.

View File

@ -0,0 +1,70 @@
The Grandest Staircase Of Them All
==================================
With the LAMBCHOP doomsday device finished, Commander Lambda is preparing to debut on the galactic stage -- but in order to make a grand entrance, Lambda needs a grand staircase! As the Commander's personal assistant, you've been tasked with figuring out how to build the best staircase EVER.
Lambda has given you an overview of the types of bricks available, plus a budget. You can buy different amounts of the different types of bricks (for example, 3 little pink bricks, or 5 blue lace bricks). Commander Lambda wants to know how many different types of staircases can be built with each amount of bricks, so they can pick the one with the most options.
Each type of staircase should consist of 2 or more steps. No two steps are allowed to be at the same height - each step must be lower than the previous one. All steps must contain at least one brick. A step's height is classified as the total amount of bricks that make up that step.
For example, when N = 3, you have only 1 choice of how to build the staircase, with the first step having a height of 2 and the second step having a height of 1: (# indicates a brick)
#
##
21
When N = 4, you still only have 1 staircase choice:
#
#
##
31
But when N = 5, there are two ways you can build a staircase from the given bricks. The two staircases can have heights (4, 1) or (3, 2), as shown below:
#
#
#
##
41
#
##
##
32
Write a function called solution(n) that takes a positive integer n and returns the number of different staircases that can be built from exactly n bricks. n will always be at least 3 (so you can have a staircase at all), but no more than 200, because Commander Lambda's not made of money!
Languages
=========
To provide a Java solution, edit Solution.java
To provide a Python solution, edit solution.py
Test 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(3)
Output:
1
Input:
Solution.solution(200)
Output:
487067745
-- Python cases --
Input:
solution.solution(200)
Output:
487067745
Input:
solution.solution(3)
Output:
1
Use 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.

View File

@ -0,0 +1,29 @@
# THE GRANDEST STAIRCASE OF THEM ALL
#
# Build the stairs from *bottom* to *top* ;P
# Works, but n=199 and n=200 takes four minutes!
def solution(n):
w, _ = ss5(n, 1, 1, 0)
return w
def ss5(n, t, start, w):
if start >= n or n-t <= 0 or n-t <= start:
# n += start
t -= start
return w, start
w += 1
newstart = start + 1
# n -= start
t += newstart
w, oldstart = ss5(n, t, newstart, w)
if oldstart != 0:
t -= start
start += 1
# t -= start
return ss5(n, t, start, w)
print(solution(200))

View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
# TODO: test values and test time