This repository was archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccess_codes.py
executable file
·124 lines (91 loc) · 3.44 KB
/
access_codes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
'''
Author: John D. Anderson (TigerJ)
Usage: access_codes -case1 | -case2 | -case3
Origin: Google "foo.bar" project - Problem 2.2
Problem Description:
Access codes
============
You've discovered the evil laboratory of Dr. Boolean, and you've found that
the vile doctor is transforming your fellow rabbit kin into terrifying
rabbit-zombies! Naturally, you're less-than-happy about this turn of
events.
Of course where there's a will, there's a way. Your top-notch assistant,
Beta Rabbit, managed to sneak in and steal some access codes for
Dr. Boolean's lab in the hopes that the two of you could then return and
rescue some of the undead rabbits. Unfortunately, once an access code is
used it cannot be used again. Worse still, if an access code is used,
then that code backwards won't work either! Who wrote this security system?
Your job is to compare a list of the access codes and find the number of
distinct codes, where two codes are considered to be "identical"
(not distinct) if they're exactly the same, or the same but reversed.
The access codes only contain the letters a-z, are all lowercase, and have
at most 10 characters. Each set of access codes provided will have at most
5000 codes in them.
For example, the access code "abc" is identical to "cba" as well as "abc."
The code "cba" is identical to "abc" as well as "cba."
The list ["abc," "cba," "bac"] has 2 distinct access codes in it.
Write a function answer(x) which takes a list of access code strings, x,
and returns the number of distinct access code strings using this
definition of identical.
Languages
=========
To provide a Python solution, edit solution.py
To provide a Java solution, edit solution.java
Test cases
==========
Inputs:
(string list) x = ["foo", "bar", "oof", "bar"]
Output:
(int) 2
Inputs:
(string list) x = ["x", "y", "xy", "yy", "", "yx"]
Output:
(int) 5
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.
'''
# libs
import sys
# functions
def answer(x):
# starting index
start = 0
while start + 1 < len(x):
# matches
matches = []
# iterate from starting point to end
for i in range(start + 1, len(x)):
if x[start] == x[i]:
matches.append(i)
elif x[start][::-1] == x[i]:
matches.append(i)
# delete matches
for i, m in enumerate(matches):
del x[m-i:m-i+1]
# next
start += 1
# return distinct access codes
return len(x)
# executable
if __name__ == '__main__':
# usage message
usage = '\nUsage: access_codes -case1 | -case2 | -case3\n'
# CLA check
if len(sys.argv) == 2:
if sys.argv[1] == '-case1':
x = ["foo", "bar", "oof", "bar"]
elif sys.argv[1] == '-case2':
x = ["x", "y", "xy", "yy", "", "yx"]
elif sys.argv[1] == '-case3':
x = ["a", "b", "c", "a", "a", "foo", "bar", "foo", "oof", "rab"]
# wrong arguments passed
else:
sys.exit(usage)
# correct arguments passed
print answer(x)
sys.exit()
# no arguments passed
sys.exit(usage)