This is the continuation of the previous tutorials of the Python programming series -
So, please refer part 1, part 2 before going any further.
Coding
String-1 > hello_name
Question:
Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".
Check for these cases:
hello_name('Bob') → 'Hello Bob!'
hello_name('Alice') → 'Hello Alice!'
hello_name('X') → 'Hello X!'
Solution:
def hello_name(name):
print("Hello " + name + "!")
hello_name('Bob') #→ 'Hello Bob!'
hello_name('Alice') #→ 'Hello Alice!'
hello_name('X') #→ 'Hello X!'
#----------------------------------------#
#----------------------------------------#
String-1 > make_abba
Question:
Given two strings, a and b, return the result of putting them together in the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi".
Check for these cases:
make_abba('Hi', 'Bye') → 'HiByeByeHi'
make_abba('Yo', 'Alice') → 'YoAliceAliceYo'
make_abba('What', 'Up') → 'WhatUpUpWhat'
Solution:
def make_abba(a, b):
print(a + b + b + a)
make_abba('Hi', 'Bye') #→ 'HiByeByeHi'
make_abba('Yo', 'Alice') #→ 'YoAliceAliceYo'
make_abba('What', 'Up') #→ 'WhatUpUpWhat'
#----------------------------------------#
#----------------------------------------#
String-1 > make_tags
Question:
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>".
Check for these cases:
make_tags('i', 'Yay') → '<i>Yay</i>'
make_tags('i', 'Hello') → '<i>Hello</i>'
make_tags('cite', 'Yay') → '<cite>Yay</cite>'
Solution:
def make_tags(tag, word):
print("<" + tag + ">" + word + "</" + tag + ">")
make_tags('i', 'Yay') #→ '<i>Yay</i>'
make_tags('i', 'Hello') #→ '<i>Hello</i>'
make_tags('cite', 'Yay') #→ '<cite>Yay</cite>'
#----------------------------------------#
#----------------------------------------#
String-1 > make_out_word
Question:
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>>".
Check for these cases:
make_out_word('<<>>', 'Yay') → '<<Yay>>'
make_out_word('<<>>', 'WooHoo') → '<<WooHoo>>'
make_out_word('[[]]', 'word') → '[[word]]'
Solution:
def make_out_word(out, word):
print(out[:2] + word + out[2:])
make_out_word('<<>>', 'Yay') #→ '<<Yay>>'
make_out_word('<<>>', 'WooHoo') #→ '<<WooHoo>>'
make_out_word('[[]]', 'word') #→ '[[word]]'
#----------------------------------------#
#----------------------------------------#
String-1 > extra_end
Question:
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.
Check for these cases:
extra_end('Hello') → 'lololo'
extra_end('ab') → 'ababab'
extra_end('Hi') → 'HiHiHi'
Solution:
def extra_end(str):
if len(str) < 2:
return str
print(str[-2:] * 3)
extra_end('Hello') #→ 'lololo'
extra_end('ab') #→ 'ababab'
extra_end('Hi') #→ 'HiHiHi'
extra_end('H')
#----------------------------------------#
#----------------------------------------#
String-1 > first_two
Question:
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 "".
Check for these cases:
first_two('Hello') → 'He'
first_two('abcdefg') → 'ab'
first_two('ab') → 'ab'
Solution:
def first_two(str):
if len(str) < 2:
return str
print(str[:2])
first_two('Hello') #→ 'He'
first_two('abcdefg') #→ 'ab'
first_two('ab') #→ 'ab'
#----------------------------------------#
#----------------------------------------#
String-1 > first_half
Question:
Given a string of even length, return the first half. So the string "WooHoo" yields "Woo".
Check for these cases:
first_half('WooHoo') → 'Woo'
first_half('HelloThere') → 'Hello'
first_half('abcdef') → 'abc'
Solution:
def first_half(str):
if len(str) % 2 != 0:
print("The length of the string is Odd.")
str_half = int(len(str)/2) # As division always return float type no, So convert it into 'int'
print(str[:str_half])
first_half('WooHoo') #→ 'Woo'
first_half('HelloThere') #→ 'Hello'
first_half('abcdef') #→ 'abc'
#----------------------------------------#
#----------------------------------------#
String-1 > without_end
Question:
Given a string, return a version without the first and last char, so "Hello" yields "ell". The string length will be at least 2.
Check for these cases:
without_end('Hello') → 'ell'
without_end('java') → 'av'
without_end('coding') → 'odin'
Solution:
def without_end(str):
if len(str) < 2:
return str
# If the num is even,
if len(str) % 2 == 0:
half_len_str = int(len(str)/2)
front = str[1:half_len_str]
rear = str[-half_len_str:-1]
print(front + rear)
# If the num is odd,
else:
half_len_str = int(len(str)/2)
front = str[1:half_len_str]
rear = str[-half_len_str-1:-1]
print(front + rear)
without_end('Hello') #→ 'ell'
without_end('java') #→ 'av'
without_end('ava') #→ 'v'
without_end('coding') #→ 'odin'
#----------------------------------------#
#----------------------------------------#
String-1 > combo_string
Question:
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).
Check for these cases:
combo_string('Hello', 'hi') → 'hiHellohi'
combo_string('hi', 'Hello') → 'hiHellohi'
combo_string('aaa', 'b') → 'baaab'
Solution:
def combo_string(a, b):
short = ""
long = ""
if len(a) < len(b):
short = a
long = b
print(short + long + short)
elif len(a) == len(b) or len(a) == len(b) == 0:
print(a + b)
else:
short = b
long = a
print(short + long + short)
combo_string('Hello', 'hi') #→ 'hiHellohi'
combo_string('hi', 'Hello') #→ 'hiHellohi'
combo_string('aaa', 'b') #→ 'baaab'
combo_string('c', 'b') #→ 'cb'
combo_string('', '') #→ ''
#----------------------------------------#
#----------------------------------------#
String-1 > non_start
Question:
Given 2 strings, return their concatenation, except omit the first char of each. The strings will be at least length 1.
Check for these cases:
non_start('Hello', 'There') → 'ellohere'
non_start('java', 'code') → 'avaode'
non_start('shotl', 'java') → 'hotlava'
Solution:
def non_start(a, b):
print(a.replace(a[0], "") + b.replace(b[0], ""))
non_start('Hello', 'There') #→ 'ellohere'
non_start('java', 'code') #→ 'avaode'
non_start('shotl', 'java') #→ 'hotlava'
#----------------------------------------#
#----------------------------------------#
String-1 > left2
Question:
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.
Check for these cases:
left2('Hello') → 'lloHe'
left2('java') → 'vaja'
left2('Hi') → 'Hi'
Solution:
def left2(str):
if len(str) >= 2:
print(str[2:] + str[:2])
else:
return -1
left2('Hello') #→ 'lloHe'
left2('java') #→ 'vaja'
left2('Hi') #→ 'Hi'
#----------------------------------------#
#----------------------------------------#
Thanks bro for sharing upvoted u can check mine n get more information.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://github.com/ozelentok/CodingBat-Solutions/blob/master/Java/String-1.java
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
That is in Java
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
hahaha
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit