|
| 1 | +''' |
| 2 | +* Programmer : Dhruv Patel |
| 3 | +* Problem Name : CodingBat.com |
| 4 | +* Used In : Python |
| 5 | +* Used As : String-1 |
| 6 | +* Thoughts => |
| 7 | +* This file will contain the solutions for the codingbat.com in attempt to have digest |
| 8 | +* for my coderschool kids.It's my own version of solutions which might be diffrent then |
| 9 | +* the actual ones. |
| 10 | +''' |
| 11 | +''' |
| 12 | + String-1 > hello_name |
| 13 | + Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!". |
| 14 | + hello_name('Bob') → 'Hello Bob!' |
| 15 | + hello_name('Alice') → 'Hello Alice!' |
| 16 | + hello_name('X') → 'Hello X!' |
| 17 | +''' |
| 18 | + |
| 19 | +def hello_name(name): |
| 20 | + return 'Hello '+name+'!' |
| 21 | + |
| 22 | +''' |
| 23 | + String-1 > make_abba |
| 24 | + Given two strings, a and b, return the result of putting them together in |
| 25 | + the order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi". |
| 26 | + make_abba('Hi', 'Bye') → 'HiByeByeHi' |
| 27 | + make_abba('Yo', 'Alice') → 'YoAliceAliceYo' |
| 28 | + make_abba('What', 'Up') → 'WhatUpUpWhat' |
| 29 | +''' |
| 30 | + |
| 31 | +def make_abba(a, b): |
| 32 | + return a+b+b+a |
| 33 | + |
| 34 | +''' |
| 35 | + String-1 > make_tags |
| 36 | + The web is built with HTML strings like "<i>Yay</i>" which draws Yay as italic text. |
| 37 | + In this example, the "i" tag makes <i> and </i> which surround the word "Yay". |
| 38 | + Given tag and word strings, create the HTML string with tags around the word, e.g. "<i>Yay</i>". |
| 39 | + make_tags('i', 'Yay') → '<i>Yay</i>' |
| 40 | + make_tags('i', 'Hello') → '<i>Hello</i>' |
| 41 | + make_tags('cite', 'Yay') → '<cite>Yay</cite>' |
| 42 | +''' |
| 43 | + |
| 44 | +def make_tags(tag, word): |
| 45 | + return '<'+tag+'>'+word+'</'+tag+'>' |
| 46 | + |
| 47 | +''' |
| 48 | + String-1 > make_out_word |
| 49 | + Given an "out" string length 4, such as "<<>>", and a word, return a new string |
| 50 | + where the word is in the middle of the out string, e.g. "<<word>>". |
| 51 | + make_out_word('<<>>', 'Yay') → '<<Yay>>' |
| 52 | + make_out_word('<<>>', 'WooHoo') → '<<WooHoo>>' |
| 53 | + make_out_word('[[]]', 'word') → '[[word]]' |
| 54 | +''' |
| 55 | + |
| 56 | +def make_out_word(out, word): |
| 57 | + return out[0]+out[1]+word+out[2]+out[3] |
| 58 | + |
| 59 | +''' |
| 60 | + String-1 > extra_end |
| 61 | + Given a string, return a new string made of 3 copies of the last 2 chars of |
| 62 | + the original string. The string length will be at least 2. |
| 63 | + extra_end('Hello') → 'lololo' |
| 64 | + extra_end('ab') → 'ababab' |
| 65 | + extra_end('Hi') → 'HiHiHi' |
| 66 | +''' |
| 67 | + |
| 68 | +def extra_end(str): |
| 69 | + return str[len(str)-2:]*3 |
| 70 | + |
| 71 | +''' |
| 72 | + String-1 > first_two |
| 73 | + Given a string, return the string made of its first two chars, so the String |
| 74 | + "Hello" yields "He". If the string is shorter than length 2, return whatever |
| 75 | + there is, so "X" yields "X", and the empty string "" yields the empty string "". |
| 76 | + first_two('Hello') → 'He' |
| 77 | + first_two('abcdefg') → 'ab' |
| 78 | + first_two('ab') → 'ab' |
| 79 | +''' |
| 80 | + |
| 81 | +def first_two(str): |
| 82 | + if len(str) < 2: |
| 83 | + return str |
| 84 | + else: |
| 85 | + return str[0:2] |
| 86 | + |
| 87 | +''' |
| 88 | + String-1 > first_half |
| 89 | + Given a string of even length, return the first half. So the string "WooHoo" yields "Woo". |
| 90 | + first_half('WooHoo') → 'Woo' |
| 91 | + first_half('HelloThere') → 'Hello' |
| 92 | + first_half('abcdef') → 'abc' |
| 93 | +''' |
| 94 | + |
| 95 | +def first_half(str): |
| 96 | + return str[:len(str)/2] |
| 97 | + |
| 98 | +''' |
| 99 | + String-1 > without_end |
| 100 | + Given a string, return a version without the first and last char, so "Hello" yields "ell". |
| 101 | + The string length will be at least 2. |
| 102 | + without_end('Hello') → 'ell' |
| 103 | + without_end('java') → 'av' |
| 104 | + without_end('coding') → 'odin' |
| 105 | +''' |
| 106 | + |
| 107 | +def without_end(str): |
| 108 | + return str[1:len(str)-1] |
| 109 | + |
| 110 | +''' |
| 111 | + String-1 > combo_string |
| 112 | + Given 2 strings, a and b, return a string of the form short+long+short, with the shorter |
| 113 | + string on the outside and the longer string on the inside. The strings will not be the |
| 114 | + same length, but they may be empty (length 0). |
| 115 | + combo_string('Hello', 'hi') → 'hiHellohi' |
| 116 | + combo_string('hi', 'Hello') → 'hiHellohi' |
| 117 | + combo_string('aaa', 'b') → 'baaab' |
| 118 | +''' |
| 119 | + |
| 120 | +def combo_string(a, b): |
| 121 | + if len(a)>len(b): |
| 122 | + return b+a+b |
| 123 | + else: |
| 124 | + return a+b+a |
| 125 | + |
| 126 | +''' |
| 127 | + String-1 > non_start |
| 128 | + Given 2 strings, return their concatenation, except omit the first char of each. |
| 129 | + The strings will be at least length 1. |
| 130 | + non_start('Hello', 'There') → 'ellohere' |
| 131 | + non_start('java', 'code') → 'avaode' |
| 132 | + non_start('shotl', 'java') → 'hotlava' |
| 133 | +''' |
| 134 | + |
| 135 | +def non_start(a, b): |
| 136 | + return a[1:]+b[1:] |
| 137 | + |
| 138 | +''' |
| 139 | + String-1 > left2 |
| 140 | + Given a string, return a "rotated left 2" version where the first 2 chars are moved to the end. |
| 141 | + The string length will be at least 2. |
| 142 | + left2('Hello') → 'lloHe' |
| 143 | + left2('java') → 'vaja' |
| 144 | + left2('Hi') → 'Hi' |
| 145 | +''' |
| 146 | + |
| 147 | +def left2(str): |
| 148 | + return str[2:len(str)]+str[0:2] |
| 149 | + |
0 commit comments