-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblems.yaml
More file actions
1541 lines (1533 loc) · 42.4 KB
/
Copy pathproblems.yaml
File metadata and controls
1541 lines (1533 loc) · 42.4 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
shorten_url:
tests:
- input: |
'https://a'
output: |
'a'
- input: |
'http://abc'
output: |
'abc'
- input: |
'abc'
output: |
'abc'
working_description: Shorten string by removing 'https://' or 'http://' from beginning
signature: |
def shorten_url(url):
typed_signature: |
def shorten_url(url: str) -> str:
code: |
def shorten_url(url):
url = ''.join(url)
if url.startswith("http://"):
return url[7:]
elif url.startswith("https://"):
return url[8:]
else:
return url
apply_operations:
tests:
- input: |
[ 1, 2, 3 ], [ '+', '*' ]
output: |
9
- input: |
[ 16, 56, 8, 5, 4 ], [ '+', '/', '-', '-' ]
output: |
0
- input: |
[ 1, 5, 2, 3 ], [ '+', '-', '%' ]
output: |
1
working_description: |
Initialize the result to the first number in nums. Iterate over ops and
the remaining numbers. Perform each operation to the current result and
the next number. Return the result.
signature: 'def apply_operations(nums, ops):'
typed_signature: |
def apply_operations(nums: List[int], ops: List[str]) -> int:
code: |
def apply_operations(nums, ops):
num = nums[0]
for i in range(len(ops)):
if ops[i] == '+':
num += nums[i+1]
elif ops[i] == '-':
num -= nums[i+1]
elif ops[i] == '*':
num *= nums[i+1]
elif ops[i] == '/':
num /= nums[i+1]
elif ops[i] == '%':
num %= nums[i+1]
return num
add_up:
tests:
- input: |
[ [ 'one', 2, 3, 4 ] ]
output: |
9
- input: |
[ [ 3, 'hello', 3.4 ] ]
output: |
6.4
- input: |
[ [ ] ]
output: |
0
#NOTE(arjun): This is a gotcha question, since isinstance(True, int) is True!
working_description: |
Add up all the elements in 2D array only if they are integer or float
signature: 'def add_up(arr):'
code: |
def add_up(n):
total = 0
for row in n:
for elem in row:
if type(elem) == int or type(elem) == float:
total += elem
return total
subtract_add:
tests:
- input: |
['1', '2', '3', '4']
output: |
-2
- input: |
['0', '2']
output: |
-2
- input: |
['0', '0']
output: |
0
- input: |
[ ]
output: |
0
working_description: |
Given a list of strings, convert to a list of integers. Return a result
that adds the even indices and subtracts the odd indices.
signature: 'def subtract_add(lst):'
code: |
def subtract_add(lst):
total = 0
for i in range(len(lst)):
if i % 2 == 0:
total = total + int(lst[i])
else:
total = total - int(lst[i])
return total
convert:
tests:
- input: |
[0, 1, 2, 3]
output: |
['ABCD']
- input: |
[0, -1, 1, -1, 2]
output: |
['A', 'B', 'C']
- input: |
[1, 1, 1, -1, 25, 25, 25, -1, 0, 1, 2]
output: |
['BBB', 'ZZZ', 'ABC']
working_description: |
lst is a list of numbers, where 0 -> A, 1 -> B, ..., and Z -> 25. Moreover,
-1 -> ' '. Build a string, then return a list of strings by splitting on
' '.
signature: 'def convert(lst):'
code: |
def convert(lst):
return ''.join([chr(i+65) if i >= 0 else " " for i in lst]).split()
order_strings:
tests:
- input: |
['c', 'b', 'a', 'd']
output: |
['b', 'a', 'd', 'c']
- input: |
[1, 2, 3, 4, 5, 6]
output: |
[3, 2, 1, 6, 5, 4]
- input: |
['hello', 'aaa', 'bbb', 'ccc']
output: |
['bbb', 'aaa', 'hello', 'ccc']
working_description: Sort lst then reverse the first and second halves
signature: 'def order_strings(lst):'
code: |
def order_strings(lst):
lst.sort()
lst[0:len(lst)//2] = lst[0:len(lst)//2][::-1]
lst[len(lst)//2:] = lst[len(lst)//2:][::-1]
return lst
add_int:
tests:
- input: |
['hello', 'aaa', 'bbb', 'ccc', 1, 4], 5
output: |
['hello5', 'aaa5', 'bbb5', 'ccc5', 6, 9]
- input: |
[8, 56, 8, '5', '4'], 1
output: |
[9, 57, 9, '51', '41']
- input: |
['hello', 'aaa', 'bbb', ['list'], 1, 4], 5
output: |
['hello5', 'aaa5', 'bbb5', ['list'], 6, 9]
working_description: |
Given a list lst of strings and integers and integer num, concatenates num to the end of strings in lst and adds it to integers in lst.
signature: 'def add_int(lst, num):'
code: |
def add_int(lst, extra):
new_lst = []
for item in lst:
if isinstance(item, int) or isinstance(item, float):
new_lst.append(item + extra)
elif isinstance(item, str):
new_lst.append(item + str(extra))
else:
new_lst.append(item)
return new_lst
check_prime:
tests:
- input: |
'2'
output: |
True
- input: |
'8'
output: |
False
- input: |
'9'
output: |
False
- input: |
'11'
output: |
True
working_description: |
Convert string argument to an int, then return True if it is prime.
signature: 'def check_prime(num):'
code: |
def check_prime(num):
num = int(num)
if num == 2:
return True
if num % 2 == 0:
return False
for i in range(3, int(num**0.5)+1, 2):
if num % i == 0:
return False
return True
remove_odd:
category: loops
keywords: [lists, loops, math]
tests:
- input: |
[4.3, 4, 5, 2, 7]
output: |
[4.3, 4, 2]
- input: |
[1.1, 2.2, 3.3]
output: |
[1.1, 2.2, 3.3]
- input: |
[3, 5, 7]
output: |
[]
working_description: 'remove all elements that are int and odd from given list'
signature: 'def remove_odd(lst):'
code: |
def remove_odd(lst):
return [x for x in lst if x % 2 != 1]
create_list:
tests:
- input: |
{'a': 'b', 't': 'd'}, ['a', 't', 'c']
output: |
['b', 'd', None]
- input: |
{1: 'a', 2: 'b', 3: 'c'}, [1, 5, 3]
output: |
['a', None, 'c']
- input: |
{3 : 't', 4: 'r', 5: 'e'}, ['a', 'b', 'c']
output: |
[None, None, None]
working_description: |
Given a dict dt and list lst, iterates through lst and appends values in dt to the result. If value is not a key in dt, appends None.
signature: 'def create_list(dt, lst):'
code: |
def create_list(d, l):
return_lst = []
for i in l:
if i in d:
return_lst.append(d[i])
else:
return_lst.append(None)
return return_lst
has_qu:
working_description: A function that receives a list of letters and and returns True if the list contains Q or U
signature: 'def has_qu(lst):'
tests:
- input: |
["A", "B", "C"]
output: |
False
- input: |
["A", "Q", "C"]
output: |
True
- input: |
["A", "U", "C"]
output: |
True
- input: |
["Q", "U"]
output: |
True
code: |
def has_qu(letters):
if "Q" in letters or "U" in letters:
return True
return False
planets_mass:
working_description: Given a dictionary of planets and their mass, remove keys 'Pluto' and 'Sun' if they are in planets and return the sum of the planets' masses.
signature: |
def planets_mass(planets):
tests:
- input: |
{'Venus': 10, 'Mercury': 5, 'Sun': 100}
output: |
15
- input: |
{'Pluto': 30, 'Venus': 10, 'Mercury': 5, 'Sun': 100}
output: |
15
- input: |
{'Pluto': 100}
output: |
0
code: |
def planets_mass(solar_system):
return sum(mass for planet, mass in solar_system.items() if planet !='Pluto' and planet !='Sun')
student_grades:
working_description: |
Given a dictionary students mapping names to id numbers and a dictionary grades mapping id numbers to lists of grades, returns a dictionary mapping names to grades using matching id numbers.
signature: |
def student_grades(students, grades):
code: |
def student_grades(students, grades):
name_dict = {}
for k,v in students.items():
name_dict[k] = grades[v]
return name_dict
tests:
- input: |
{'Alice': 20, 'Bob': 11}, {20: ['A','A'], 11: ['A','B'], 21: ['B']}
output: |
{'Alice': ['A','A'], 'Bob': ['A','B']}
- input: |
{'Alice': 20}, {20: [], 11: ['A','B'], 21: ['A-']}
output: |
{'Alice': []}
- input: |
{'Sam': 21, 'Ari': 67, 'Chris': 23}, {67: [], 23: ['A'], 21: ['B','B']}
output: |
{'Sam': ['B', 'B'], 'Ari':[], 'Chris':['A'] }
times_with:
working_description: |
Given a list of meetings, where each meeting is a list containing a duration and a list of attendees,
return the time with each person as a dictionary.
signature: |
def times_with(meetings):
code: |
def times_with(meetings):
time_with = {}
for meeting in meetings:
for attendee in meeting[1]:
if attendee not in time_with:
time_with[attendee] = 0
time_with[attendee] += meeting[0]
return time_with
tests:
- input: |
[ [30, ['Alice', 'Bob', 'Carol']] ]
output: |
{ 'Alice': 30, 'Bob': 30, 'Carol': 30 }
- input: |
[ [30, ['Alice', 'Bob', 'Carol']], [20, ['Alice', 'Bob']] ]
output: |
{ 'Alice': 50, 'Bob': 50, 'Carol': 30 }
- input: |
[ [10, ['Alice', 'Bob']], [5, ['Carol']] ]
output: |
{'Alice': 10, 'Bob': 10, 'Carol': 5}
pig_latin:
working_description: |
Returns a list of words from words modified as follows:
1. Words shorter than 3 letters are unchanged.
2. Words starting with a vowel have 'ay' concatenated to the end.
3. The first letter is concatenated to the end followed by 'ay'.
signature: |
def pig_latin(words):
code: |
def pig_latin(words):
result = []
for word in words:
if len(word) < 3:
result.append(word)
elif word[0] in 'AEIOUaeiou':
result.append(word + 'ay')
else:
result.append(word[1:] + word[0] + 'ay')
return result
tests:
- input: |
['this', 'is', 'a', 'great', 'example']
output: |
['histay', 'is', 'a', 'reatgay', 'exampleay']
- input: |
['hello']
output: |
['ellohay']
- input: |
['bye']
output: |
['yebay']
count_consecutive:
working_description: |
Takes in one parameter: 1) a string, and returns an integer representing
the maximum number that a letter appears consecutively in the string.
signature: |
def count_consecutive(word):
code: |
def count_consecutive(word):
current = word[0]
count = 1
maxcount = 1
for letter in word[1:]:
if letter == current:
count += 1
else:
current = letter
maxcount = max(maxcount, count)
count = 1
return maxcount
tests:
- input: |
'elephant'
output: |
1
- input: |
'cheese'
output: |
2
- input: |
'helllloo'
output: |
4
meeps_morps:
working_description: |
Returns a list where the first index is the number of times 'Meep' appears in the list, and the second index is the number of times 'Morp' appears in the list.
signature: |
def meeps_morps(items):
code: |
def meeps_morps(items):
meeps = 0
morps = 0
for word in items:
if word == 'Meep':
meeps += 1
if word == 'Morp':
morps += 1
return [meeps, morps]
tests:
- input: |
['Meep', 'Morp', 'Meep', 'Meep', 'Suitcase', 'Kind', 'Morp']
output: |
[3,2]
- input: |
['Meep', 'Meep', 'Suitcase', 'Kind']
output: |
[2,0]
- input: |
['Suitcase', 'Kind', 'Carrot', 'Meat', 'Morpling']
output: |
[0,0]
print_time:
code: |
def print_time(day,hour):
if day in 'Sat Sun':
return 'weekend'
if(0 <= hour <= 8):
return 'sleeping'
else:
return 'weekday'
signature: |
def print_time(day,hour):
working_description: |
Given a weekday and time, returns a string:
1. 'weekend' for 'Sat' or 'Sun'
2. 'weekday' for times between 9 and 23 inclusive
3. 'sleeping' otherwise
tests:
- input: |
'Sat', 12
output: |
'weekend'
- input: |
'Sun', 12
output: |
'weekend'
- input: |
'Mon', 9
output: |
'weekday'
- input: |
'Tue', 23
output: |
'weekday'
- input: |
'Wed', 0
output: |
'sleeping'
- input: |
'Thu', 8
output: |
'sleeping'
mod_end:
working_description: |
Takes two parameters: 1) a list of words, and 2) a word ending, and returns a new list replacing the old ending with 's'.
signature: |
def mod_end(words, ending):
code: |
def mod_end(words, ending):
result = []
for word in words:
result.append('s'.join(word.rsplit(ending,1)))
return result
tests:
- input: |
['jumping', 'bumping', 'singing', 'picking'], 'ing'
output: |
['jumps', 'bumps', 'sings', 'picks']
- input: |
['dreaded', 'edited', 'heated'], 'ed'
output: |
['dreads', 'edits', 'heats']
- input: |
['nearer', 'closer'], 'er'
output: |
['nears', 'closs']
increaseScore:
working_description: |
For values less than 10, returns 10. Otherwise, return score + 1.
signature: |
def increaseScore(score):
code: |
def increaseScore(score):
if score < 10:
score = 10
else:
score += 1
return score
tests:
- input: |
-10
output: |
10
- input: |
1
output: |
10
- input: |
10
output: |
11
- input: |
15
output: |
16
- input: |
20
output: |
21
getSeason:
working_description: |
Takes in a string argument, month, and returns the season that the month is in.
December to February are winter, March to May are spring, and so on. The
returned season should be in lowercase.
signature: |
def getSeason(month):
code: |
def getSeason(month):
if month in 'December January February':
return 'winter'
elif month in 'March April May':
return 'spring'
elif month in 'June July August':
return 'summer'
else:
return 'fall'
tests:
- input: |
'January'
output: |
'winter'
- input: |
'April'
output: |
'spring'
- input: |
'August'
output: |
'summer'
- input: |
'October'
output: |
'fall'
assessVowels:
working_description: |
Takes string as a parameter. The function should then create a list, and add all uppercase or lowercase vowels that appear in the string to the list (the vowels can repeat in the list). It should then return the list.
signature: |
def assessVowels(s):
code: |
def assessVowels(s):
result = []
for letter in s:
if letter in 'AEIOUaeiou':
result.append(letter)
return result
tests:
- input: |
'I ate an apple'
output: |
['I', 'a', 'e', 'a', 'a', 'e']
- input: |
'Another string here'
output: |
['A', 'o', 'e', 'i', 'e', 'e']
- input: |
'cAbbAge'
output: |
['A', 'A', 'e']
- input: |
''
output: |
[]
hasHorizontalWin:
working_description: |
Returns True if one of the rows in board has five of letter.
signature: |
def hasHorizontalWin(board, letter):
code: |
def hasHorizontalWin(board, letter):
current = False
for row in board:
count = 0
for i in row:
if i == letter:
count += 1
else:
count = 0
if count == 5:
return True
return False
tests:
- input: |
[['', '', 'X', 'O', 'X'],
['X', 'X', 'X', 'X', 'X'],
['X', 'O', 'X', 'O', 'O'],
['', '', '', 'X', 'O'],
['', '', '', '', 'O']],
'X'
output: |
True
- input: |
[['', '', 'X', 'O', 'X'],
['X', 'X', 'X', 'X', 'O'],
['X', 'X', 'X', 'O', 'O'],
['', '', '', 'X', 'O'],
['', '', '', '', 'O']],
'X'
output: |
False
- input: |
[['', '', 'X', 'O', 'X'],
['X', 'X', 'O', 'X', 'O'],
['X', 'O', 'X', 'O', 'O'],
['', '', '', 'X', 'O'],
['', '', '', '', 'O']],
'X'
output: |
False
readingIceCream:
working_description: |
Write a function called readingIceCream to read in a TSV files containing
information about Trulys ice cream flavors. The first field is the flavor
name, the second field is the ice cream type, and the third field records
how many servings the store has left (a float). Your function should take a
list of strings representing rows from a 3-field tsv file. It should the
total number of servings of ice cream that Trulys currently has in stock.
signature: |
def readingIceCream(lines):
code: |
def readingIceCream(lines):
data = [line.split('\t') for line in lines]
servings = [float(line[2]) for line in data]
return sum(servings)
tests:
- input: |
["peppermint\tice cream\t3",
"lemon\tfrozen yogurt\t10",
"pumpkin\tice cream\t2.5",
"monster\tice cream\t21"]
output: |
36.5
- input: |
["salty\tfrozen yogurt\t10"]
output: |
10
- input: |
["salty\tfrozen yogurt\t0"]
output: |
0
findHorizontals:
working_description: |
Given a list puzzle of rows, and a list wordList of words, returns a dictionary where the keys are found words (backwards and forwards) and the values are the row in puzzle where they're found.
signature: |
def findHorizontals(puzzle,wordList):
code: |
def findHorizontals(puzzle,wordList):
found = {}
for word in wordList:
for i,line in enumerate(puzzle):
if word in line or word[::-1] in line:
found[word] = i
return found
tests:
- input: |
["ztac","zzzz","teaz","part","zzzz"],
["cat","dog","tea","party"]
output: |
{"cat":0, "tea":2}
- input: |
["ztac","zzzz","teaz","part","zzzz"],
["to","apple","ape","party"]
output: |
{}
- input: |
["as", "be", "sa", "it"],
["it"]
output: |
{'it': 3}
partialWordle:
working_description: |
Returns a list of scores. For each guess in guessList, add one point for letters that appear in hiddenWord, and two points for letters in the same index as hiddenWord.
signature: |
def partialWordle(guessList,hiddenWord):
code: |
def partialWordle(guessList,hiddenWord):
scores = []
for guess in guessList:
score = 0
for i,l in enumerate(guess):
if l in hiddenWord:
score += 1
if hiddenWord[i] == l:
score += 1
scores.append(score)
return scores
tests:
- input: |
["zzzz","bbbb","lyre","part","apes"],
'pare'
output: |
[0,0,4,6,3]
- input: |
["aaaa","wall","woah","deal","howl"],
"word"
output: |
[0,2,4,1,3]
- input: |
["real","hill","ream","meal","curl"],
"roll"
output: |
[4,4,2,2,3]
andCount:
working_description: |
Returns the number of '&' in andGrid
signature: |
def andCount(andGrid):
code: |
def andCount(andGrid):
"""
Takes a list of strings representing a grid full of dashes and/or
ampersands. Prints the number of ampersands the grid.
"""
count = 0
for row in andGrid:
for letter in row:
if letter == '&':
count += 1
return count
tests:
- input: |
['--&-','----','-&--','---&',]
output: |
3
- input: |
['&&&-','-----','--&&&','-----',]
output: |
6
- input: |
['----', '----', '----', '----']
output: |
0
set_chars:
working_description:
Write a function set_chars that returns a string where all the
indices in list l in string s are set to character c.
signature: |
def set_chars(s,c,l):
code: |
def set_chars(s,c,l):
for i in l:
if i < len(s):
s = s[:i] + c + s[i+1:]
return s
tests:
- input: |
'Everybody loves apples.', "u", [0,2,6,16]
output: |
'uvurybudy loves upples.'
- input: |
'ice cream is the best', 'q', [1,21]
output: |
'iqe cream is the best'
- input: |
'toast and jam and toast and ham', 't', []
output: |
'toast and jam and toast and ham'
altText:
working_description: Makes every other letter in s uppercase
signature: |
def altText(s):
code: |
def altText(s):
result = ''
for i in range(len(s)):
if i%2 == 0:
result += s[i].upper()
else:
result += s[i]
return result
tests:
- input: |
'I love to see dolphins swim.'
output: |
'I LoVe tO SeE DoLpHiNs sWiM.'
- input: |
'computers'
output: |
'CoMpUtErS'
- input: |
't'
output: |
'T'
changeSection:
working_description: |
Write a function that reverses everything in the string s up to (but not including) index i.
signature: |
def changeSection(s,i):
code: |
def changeSection(s,i):
flipped = s[i-1::-1]
return flipped + s[i:]
tests:
- input: |
'Turn towards the door.', 9
output: |
'awot nruTrds the door.'
- input: |
'the tall sail', 13
output: |
'lias llat eht'
- input: |
'establishing', 1
output: |
'establishing'
edit_col:
working_description: For every list in mat, sets index i to n. Returns mat.
signature: |
def edit_col(mat,i,n):
code: |
def edit_col(mat,i,n):
for row in mat:
row[i] = n
return mat
tests:
- input: |
[[1,2,3], [4,5,6], [7,8,9]], 1, 6
output: |
[[1,6,3], [4,6,6], [7,6,9]]
- input: |
[[1,1,1,1,1], [1,1,1,1,1]], 0, 9
output: |
[[9,1,1,1,1], [9,1,1,1,1]]
- input: |
[[2,3]], 1, 1
output: |
[[2,1]]
combine:
working_description: Given a 2d array, joins corresponding nested lists.
signature: |
def combine(l1,l2):
code: |
def combine(l1,l2):
result = []
for i in range(len(l1)):
result.append(l1[i] + (l2[i]))
return result
tests:
- input: |
[[1,2,3],[4,5,6],[7,8,9]],[[10,20,30],[40,50,60],[70,80,90]]
output: |
[[1,2,3,10,20,30],[4,5,6,40,50,60],[7,8,9,70,80,90]]
- input: |
[[9,8,7]], [[0,1,2]]
output: |
[[9,8,7,0,1,2]]
- input: |
[[2,3],[1,1],[10,11],[4, 5]],[[1, 1],[14, 15],[3, 2],[77, 87]]
output: |
[[2,3,1,1],[1,1,14,15],[10,11,3,2],[4,5,77,87]]
pattern:
working_description: Unknown, need to generate
signature: |
def pattern(value):
code: |
def pattern(value):
biglst = []
for i in range(value):
newlst = []
for j in range(value):
newlst.append(j+1)
biglst.append(newlst)
return biglst
tests:
- input: |
3
output: |
[[1,2,3],[1,2,3],[1,2,3]]
- input: |
4
output: |
[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
- input: |
2
output: |
[[1,2],[1,2]]
reduce:
working_description: |
Replace '^' with '_' in val.
signature: |
def reduce(val):
code: |
def reduce(val):
for i in range(len(val)):
if val[i] == '^':
val = val[:i] + '_' + val[i+1:]
return val
tests:
- input: |
'^^1-1^^'
output: |
'__1-1__'
- input: |
'~^--^~'
output: |
'~_--_~'
- input: |
'|O.0|'
output: |
'|O.0|'
fib:
working_description: Return True if n is in the Fibonacci sequence, False otherwise.
signature: |
def fib(n):
code: |
def fib(n):
a = 5 * n**2 + 4
b = 5 * n**2 -4
return round(a**0.5)**2 == a or round(b**0.5)**2 == b
tests:
- input: |
1
output: |
True
- input: |
13
output: |
True
- input: |
21
output: |
True
- input: |
19
output: |
False
add_word:
working_description: If word is in map, appends line_number to its value. If word is not in map, adds word as a key and line_number as its value. Returns map.
signature: |
def add_word(word, line_number, map):
code: |
def add_word(word, line_number, map):
if word not in map:
map[word] = [line_number]
else:
map[word].append(line_number)
return map
tests:
- input: |
'bright', 1, {'twas': [1]}
output: |