Skip to content

Commit ec79024

Browse files
committed
String-1 completed
1 parent c750d06 commit ec79024

File tree

11 files changed

+132
-0
lines changed

11 files changed

+132
-0
lines changed

String-1/combo_string.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Given 2 strings, a and b,
2+
# return a string of the form short+long+short,
3+
# with the shorter string on the outside and the longer string on the inside.
4+
# The strings will not be the same length, but they may be empty (length 0).
5+
6+
7+
# combo_string('Hello', 'hi') → 'hiHellohi'
8+
# combo_string('hi', 'Hello') → 'hiHellohi'
9+
# combo_string('aaa', 'b') → 'baaab'
10+
11+
def combo_string(a, b):
12+
if len(a) > len(b):
13+
return b + a + b
14+
else:
15+
return a + b + a

String-1/extra_end.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Given a string, return a new string made of 3 copies of the last 2 chars of the original string.
2+
# The string length will be at least 2.
3+
4+
5+
# extra_end('Hello') → 'lololo'
6+
# extra_end('ab') → 'ababab'
7+
# extra_end('Hi') → 'HiHiHi'
8+
9+
def extra_end(str):
10+
return (str[-2] + str[-1]) * 3

String-1/first_half.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Given a string of even length, return the first half.
2+
# So the string "WooHoo" yields "Woo".
3+
4+
# first_half('WooHoo') → 'Woo'
5+
# first_half('HelloThere') → 'Hello'
6+
# first_half('abcdef') → 'abc'
7+
8+
def first_half(str):
9+
h = len(str) / 2
10+
return str[: h]

String-1/first_two.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Given a string, return the string made of its first two chars,
2+
# so the String "Hello" yields "He".
3+
# If the string is shorter than length 2,
4+
# return whatever there is, so "X" yields "X",
5+
# and the empty string "" yields the empty string "".
6+
7+
8+
# first_two('Hello') → 'He'
9+
# first_two('abcdefg') → 'ab'
10+
# first_two('ab') → 'ab'
11+
12+
def first_two(str):
13+
if len(str) <= 2:
14+
return str
15+
else:
16+
return str[0] + str[1]

String-1/hello_name.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Given a string name, e.g. "Bob",
2+
# return a greeting of the form "Hello Bob!".
3+
4+
5+
# hello_name('Bob') → 'Hello Bob!'
6+
# hello_name('Alice') → 'Hello Alice!'
7+
# hello_name('X') → 'Hello X!'
8+
9+
def hello_name(name):
10+
s = "Hello " + name + "!"
11+
return s
12+
#return "Hello " + name + "!"

String-1/left2.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given a string, return a "rotated left 2"
2+
# version where the first 2 chars are moved to the end.
3+
# The string length will be at least 2.
4+
5+
6+
# left2('Hello') → 'lloHe'
7+
# left2('java') → 'vaja'
8+
# left2('Hi') → 'Hi'
9+
10+
def left2(str):
11+
return str[2:] + str[:2]

String-1/make_abba.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Given two strings, a and b,
2+
# return the result of putting them together in the order abba,
3+
# e.g. "Hi" and "Bye" returns "HiByeByeHi".
4+
5+
6+
# make_abba('Hi', 'Bye') → 'HiByeByeHi'
7+
# make_abba('Yo', 'Alice') → 'YoAliceAliceYo'
8+
# make_abba('What', 'Up') → 'WhatUpUpWhat'
9+
10+
def make_abba(a, b):
11+
return a + b * 2 + a

String-1/make_out_word.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Given an "out" string length 4,
2+
# such as "<<>>", and a word,
3+
# return a new string where the word is in the middle of the out string,
4+
# e.g. "<<word>>".
5+
6+
7+
# make_out_word('<<>>', 'Yay') → '<<Yay>>'
8+
# make_out_word('<<>>', 'WooHoo') → '<<WooHoo>>'
9+
# make_out_word('[[]]', 'word') → '[[word]]'
10+
11+
def make_out_word(out, word):
12+
return out[0] + out[1] + word + out[2] + out[3]

String-1/make_tags.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# The web is built with HTML strings like "<i>Yay</i>"
2+
# which draws Yay as italic text. In this example,
3+
# the "i" tag makes <i> and </i> which surround the word "Yay".
4+
# Given tag and word strings, create the HTML string with tags around the word,
5+
# e.g. "<i>Yay</i>".
6+
7+
8+
# make_tags('i', 'Yay') → '<i>Yay</i>'
9+
# make_tags('i', 'Hello') → '<i>Hello</i>'
10+
# make_tags('cite', 'Yay') → '<cite>Yay</cite>'
11+
12+
def make_tags(tag, word):
13+
return "<" + tag + ">" + word + "</" + tag + ">"

String-1/non_start.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Given 2 strings, return their concatenation,
2+
# except omit the first char of each.
3+
# The strings will be at least length 1.
4+
5+
6+
# non_start('Hello', 'There') → 'ellohere'
7+
# non_start('java', 'code') → 'avaode'
8+
# non_start('shotl', 'java') → 'hotlava'
9+
10+
11+
def non_start(a, b):
12+
return a[1:] + b[1:]

0 commit comments

Comments
 (0)