From 941bac0dc5e52852eacf879e2d2539340123207a Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sat, 7 May 2022 18:22:29 +0000 Subject: [PATCH] finish extra/en-route-salute --- README.md | 3 ++ extra/en-route-salute/constraints.txt | 21 ++++++++++++ extra/en-route-salute/readme.txt | 48 +++++++++++++++++++++++++++ extra/en-route-salute/solution.py | 19 +++++++++++ 4 files changed, 91 insertions(+) create mode 100644 extra/en-route-salute/constraints.txt create mode 100644 extra/en-route-salute/readme.txt create mode 100644 extra/en-route-salute/solution.py diff --git a/README.md b/README.md index e5abfe6..e4ff0f8 100644 --- a/README.md +++ b/README.md @@ -82,3 +82,6 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM ### ion-flux-relabeling * Completed in: 9 hrs, 33 mins, 36 secs. + +### en-route-salute +* Completed in: 7 mins, 15 secs. diff --git a/extra/en-route-salute/constraints.txt b/extra/en-route-salute/constraints.txt new file mode 100644 index 0000000..f201987 --- /dev/null +++ b/extra/en-route-salute/constraints.txt @@ -0,0 +1,21 @@ +Java +==== +Your code will be compiled using standard Java 8. All tests will be run by calling the solution() method inside the Solution class + +Execution time is limited. + +Wildcard imports and some specific classes are restricted (e.g. java.lang.ClassLoader). You will receive an error when you verify your solution if you have used a blacklisted class. + +Third-party libraries, input/output operations, spawning threads or processes and changes to the execution environment are not allowed. + +Your solution must be under 32000 characters in length including new lines and and other non-printing characters. + +Python +====== +Your code will run inside a Python 2.7.13 sandbox. All tests will be run by calling the solution() function. + +Standard libraries are supported except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib. + +Input/output operations are not allowed. + +Your solution must be under 32000 characters in length including new lines and and other non-printing characters. diff --git a/extra/en-route-salute/readme.txt b/extra/en-route-salute/readme.txt new file mode 100644 index 0000000..62218ce --- /dev/null +++ b/extra/en-route-salute/readme.txt @@ -0,0 +1,48 @@ +En Route Salute +=============== + +Commander Lambda loves efficiency and hates anything that wastes time. The Commander is a busy lamb, after all! Henchmen who identify sources of inefficiency and come up with ways to remove them are generously rewarded. You've spotted one such source, and you think solving it will help you build the reputation you need to get promoted. + +Every time the Commander's employees pass each other in the hall, each of them must stop and salute each other -- one at a time -- before resuming their path. A salute is five seconds long, so each exchange of salutes takes a full ten seconds (Commander Lambda's salute is a bit, er, involved). You think that by removing the salute requirement, you could save several collective hours of employee time per day. But first, you need to show the Commander how bad the problem really is. + +Write a program that counts how many salutes are exchanged during a typical walk along a hallway. The hall is represented by a string. For example: +"--->-><-><-->-" + +Each hallway string will contain three different types of characters: '>', an employee walking to the right; '<', an employee walking to the left; and '-', an empty space. Every employee walks at the same speed either to right or to the left, according to their direction. Whenever two employees cross, each of them salutes the other. They then continue walking until they reach the end, finally leaving the hallway. In the above example, they salute 10 times. + +Write a function solution(s) which takes a string representing employees walking along a hallway and returns the number of times the employees will salute. s will contain at least 1 and at most 100 characters, each one of -, >, or <. + +Languages +========= + +To provide a Python solution, edit solution.py +To provide a Java solution, edit Solution.java + +Test 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(">----<") +Output: + 2 + +Input: +solution.solution("<<>><") +Output: + 4 + +-- Java cases -- +Input: +Solution.solution("<<>><") +Output: + 4 + +Input: +Solution.solution(">----<") +Output: + 2 + +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. diff --git a/extra/en-route-salute/solution.py b/extra/en-route-salute/solution.py new file mode 100644 index 0000000..8b18e9c --- /dev/null +++ b/extra/en-route-salute/solution.py @@ -0,0 +1,19 @@ +def solution(s): + result = 0 + count = 0 + for c in s: + if c == '>': + count += 1 + if c == '<': + result += count + return 2 * result + +tests = [ + (">----<", 2), + ("<<>><", 4), + ("--->-><-><-->-", 10), + ] + +for i, o in tests: + result = solution(i) + print (i, result == o, result, o)