FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codingbat-python-solutions/string-1.py at master · matthiaskoster/codingbat-python-solutions · GitHub
matthiaskoster
/
codingbat-python-solutions
Public
forked from
diezguerra/codingbat-python-solutions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
codingbat-python-solutions
/
string-1.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
80 lines (70 loc) · 2.4 KB
Breadcrumbs
codingbat-python-solutions
/
string-1.py
Copy path
File metadata and controls
80 lines (70 loc) · 2.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
def
hello_name
(
name
):
"""
Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".
"""
return
"Hello "
+
name
+
"!"
def
make_abba
(
a
,
b
):
"""
Given two strings, a and b, return the result of putting them together
in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi".
"""
return
a
+
2
*
b
+
a
def
make_tags
(
tag
,
word
):
"""
The web is built with HTML strings like "<i>Yay</i>" which draws Yay as
italic text. In this example, the "i" tag makes <i> and </i> which surround
the word "Yay". Given tag and word strings, create the HTML string with tags
around the word, e.g. "<i>Yay</i>".
"""
return
"<"
+
tag
+
">"
+
word
+
"</"
+
tag
+
">"
def
make_out_word
(
out
,
word
):
"""
Given an "out" string length 4, such as "<<>>", and a word, return a new
string where the word is in the middle of the out string, e.g. "<<word>>".
"""
return
out
[:
2
]
+
word
+
out
[
2
:]
def
extra_end
(
str
):
"""
Given a string, return a new string made of 3 copies of the last 2 chars
of the original string. The string length will be at least 2.
"""
return
str
[
-
2
:]
*
3
def
first_two
(
str
):
"""
Given a string, return the string made of its first two chars, so the
String "Hello" yields "He". If the string is shorter than length 2, return
whatever there is, so "X" yields "X", and the empty string "" yields the
empty string "".
"""
return
str
if
len
(
str
)
<=
2
else
str
[:
2
]
def
first_half
(
str
):
"""
Given a string of even length, return the first half. So the string "WooHoo"
yields "Woo".
"""
return
str
[:
len
(
str
)
/
2
]
def
without_end
(
str
):
"""
Given a string, return a version without the first and last char,
so "Hello" yields "ell". The string length will be at least 2.
"""
return
str
[
1
:
-
1
]
def
combo_string
(
a
,
b
):
"""
Given 2 strings, a and b, return a string of the form short+long+short,
with the shorter string on the outside and the longer string on the inside.
The strings will not be the same length, but they may be empty (length 0).
"""
return
a
+
b
+
a
if
len
(
a
)
<
len
(
b
)
else
b
+
a
+
b
def
non_start
(
a
,
b
):
"""
Given 2 strings, return their concatenation, except omit the first char
of each. The strings will be at least length 1.
"""
return
a
[
1
:]
+
b
[
1
:]
def
left2
(
str
):
"""
Given a string, return a "rotated left 2" version where the first 2 chars
are moved to the end. The string length will be at least 2.
"""
return
str
[
2
:]
+
str
[:
2
]
Back
|
FazBrowse Home
|
New Git URL