├── Python ├── __init__.py ├── chapter01 │ ├── __init__.py │ ├── p01_is_unique │ │ ├── __init__.py │ │ ├── ismael_duran.py │ │ ├── camilosalazar98.py │ │ ├── lomeli_noe.py │ │ ├── nickolasteixeira.py │ │ └── miguelHx.py │ ├── p02_check_perm │ │ ├── __init__.py │ │ ├── lomeli_noe.py │ │ ├── ismael_duran.py │ │ ├── camilosalazar98.py │ │ ├── nickolasteixeira.py │ │ └── miguelHx.py │ ├── p03_urlify │ │ ├── __init__.py │ │ ├── ismael_duran.py │ │ ├── nickolasteixeira.py │ │ └── camilosalazar98.py │ ├── p04_palinperm │ │ ├── __init__.py │ │ ├── camilosalazar98.py │ │ └── nickolasteixeira.py │ ├── p05_oneaway │ │ ├── __init__.py │ │ ├── nickolasteixeira.py │ │ ├── camilosalazar98.py │ │ └── miguelHx.py │ ├── p07_rotate_matrix │ │ ├── __init__.py │ │ ├── camilosalazar98.py │ │ └── nickolasteixeira.py │ ├── p08_zero_matrix │ │ ├── __init__.py │ │ ├── nickolasteixeira.py │ │ └── camilosalazar98.py │ ├── p09_string_rotation │ │ ├── __init__.py │ │ ├── nickolasteixeira.py │ │ ├── camilosalazar98.py │ │ └── miguelHx.py │ └── p06_string_compression │ │ ├── __init__.py │ │ ├── camilosalazar98.py │ │ ├── nickolasteixeira.py │ │ └── miguelHx.py ├── chapter02 │ ├── __init__.py │ ├── p04_partition │ │ ├── __init__.py │ │ └── nickolasteixeira.py │ ├── p05_sum_lists │ │ ├── __init__.py │ │ └── nickolasteixeira.py │ ├── p06_palindrome │ │ ├── __init__.py │ │ ├── nickolasteixeira.py │ │ └── camilosalazar98.py │ ├── p01_remove_dups │ │ ├── __init__.py │ │ └── camilosalazar98.py │ ├── p07_intersection │ │ └── __init__.py │ ├── p08_loop_detection │ │ ├── __init__.py │ │ ├── nickolasteixeira.py │ │ └── camilosalazar98.py │ ├── p02_return_kth_to_last │ │ ├── __init__.py │ │ ├── camilosalazar98.py │ │ └── nickolasteixeira.py │ └── p03_delete_middle_node │ │ └── __init__.py ├── chapter03 │ ├── __init__.py │ ├── p02_stack_min │ │ ├── __init__.py │ │ ├── camilosalazar98.py │ │ └── nickolasteixeira.py │ ├── p05_sort_stack │ │ ├── __init__.py │ │ └── nickolasteixeira.py │ ├── p01_three_in_one │ │ ├── __init__.py │ │ ├── camilosalazar98.py │ │ └── nickolasteixeira.py │ ├── p03_stack_of_plates │ │ └── __init__.py │ ├── p06_animal_shelter │ │ ├── __init__.py │ │ └── nickolasteixeira.py │ └── p04_queue_via_stacks │ │ ├── __init__.py │ │ └── nickolasteixeira.py ├── chapter04 │ ├── __init__.py │ ├── p06_successor │ │ └── __init__.py │ ├── p02_minimal_tree │ │ └── __init__.py │ ├── p03_list_of_depths │ │ └── __init__.py │ ├── p04_check_balanced │ │ └── __init__.py │ ├── p05_validate_bst │ │ └── __init__.py │ ├── p07_build_order │ │ └── __init__.py │ ├── p09_bst_sequences │ │ └── __init__.py │ ├── p10_check_subtree │ │ └── __init__.py │ ├── p11_random_node │ │ └── __init__.py │ ├── p12_paths_with_sum │ │ └── __init__.py │ ├── p01_route_between_nodes │ │ └── __init__.py │ └── p08_first_common_ancestor │ │ └── __init__.py ├── chapter05 │ ├── __init__.py │ ├── p01_insertion │ │ └── __init__.py │ ├── p05_debugger │ │ └── __init__.py │ ├── p06_conversion │ │ └── __init__.py │ ├── p08_draw_line │ │ └── __init__.py │ ├── p03_flip_bit_to_win │ │ └── __init__.py │ ├── p04_next_number │ │ └── __init__.py │ ├── p07_pairwise_swap │ │ └── __init__.py │ └── p02_binary_to_string │ │ └── __init__.py ├── chapter06 │ ├── __init__.py │ ├── p01_heavy_pill │ │ └── __init__.py │ ├── p02_basketball │ │ └── __init__.py │ ├── p03_dominos │ │ └── __init__.py │ ├── p10_poison │ │ └── __init__.py │ ├── p05_jugs_of_water │ │ └── __init__.py │ ├── p07_the_apocalypse │ │ └── __init__.py │ ├── p09_100_lockers │ │ └── __init__.py │ ├── p04_ants_on_a_triangle │ │ └── __init__.py │ ├── p06_blue_eyed_island │ │ └── __init__.py │ └── p08_the_egg_drop_problem │ │ └── __init__.py ├── chapter07 │ ├── __init__.py │ ├── p03_jukebox │ │ └── __init__.py │ ├── p06_jigsaw │ │ └── __init__.py │ ├── p08_othello │ │ └── __init__.py │ ├── p12_hash_table │ │ └── __init__.py │ ├── p01_deck_of_cards │ │ └── __init__.py │ ├── p02_call_center │ │ └── __init__.py │ ├── p04_parking_lot │ │ └── __init__.py │ ├── p07_chat_server │ │ └── __init__.py │ ├── p09_circular_array │ │ └── __init__.py │ ├── p10_minesweeper │ │ └── __init__.py │ ├── p11_file_system │ │ └── __init__.py │ └── p05_online_book_reader │ │ └── __init__.py ├── chapter08 │ ├── __init__.py │ ├── p11_coins │ │ └── __init__.py │ ├── p04_power_set │ │ └── __init__.py │ ├── p09_parens │ │ └── __init__.py │ ├── p10_paint_fill │ │ └── __init__.py │ ├── p01_triple_step │ │ └── __init__.py │ ├── p02_robot_in_a_grid │ │ └── __init__.py │ ├── p03_magic_index │ │ └── __init__.py │ ├── p06_towers_of_hanoi │ │ └── __init__.py │ ├── p12_eight_queens │ │ └── __init__.py │ ├── p13_stack_boxes │ │ └── __init__.py │ ├── p14_boolean_eval │ │ └── __init__.py │ ├── p05_recursive_multiply │ │ └── __init__.py │ ├── p07_permutations_without_dups │ │ └── __init__.py │ └── p08_permutations_with_dups │ │ └── __init__.py ├── chapter09 │ ├── __init__.py │ ├── p05_cache │ │ └── __init__.py │ ├── p01_stock_data │ │ └── __init__.py │ ├── p06_sales_rank │ │ └── __init__.py │ ├── p08_paste_bin │ │ └── __init__.py │ ├── p02_social_network │ │ └── __init__.py │ ├── p03_web_crawler │ │ └── __init__.py │ ├── p04_duplicate_urls │ │ └── __init__.py │ └── p07_personal_financial_manager │ │ └── __init__.py └── chapter10 │ ├── __init__.py │ ├── p01_sorted_merge │ └── __init__.py │ ├── p02_group_anagrams │ └── __init__.py │ ├── p05_sparse_search │ └── __init__.py │ ├── p06_sort_big_file │ └── __init__.py │ ├── p07_missing_int │ └── __init__.py │ ├── p08_find_duplicates │ └── __init__.py │ ├── p09_sorted_matrix_search │ └── __init__.py │ ├── p10_rank_from_stream │ └── __init__.py │ ├── p11_peaks_and_valleys │ └── __init__.py │ ├── p03_search_in_rotated_array │ └── __init__.py │ └── p04_sorted_search_no_size │ └── __init__.py ├── Go ├── chapter01 │ ├── p03_urlify │ │ └── empty.txt │ ├── p01_is_unique │ │ └── empty.txt │ ├── p02_check_perm │ │ └── empty.txt │ ├── p04_palinperm │ │ └── empty.txt │ ├── p05_oneaway │ │ └── empty.txt │ ├── p08_zero_matrix │ │ └── empty.txt │ ├── p07_rotate_matrix │ │ └── empty.txt │ ├── p09_string_rotation │ │ └── empty.txt │ └── p06_string_compression │ │ └── empty.txt ├── chapter06 │ ├── p10_poison │ │ └── empty.txt │ ├── p01_heavy_pill │ │ └── empty.txt │ ├── p02_basketball │ │ └── empty.txt │ ├── p03_dominos │ │ └── empty.txt │ ├── p09_100_lockers │ │ └── empty.txt │ ├── p05_jugs_of_water │ │ └── empty.txt │ ├── p06_blue_eyed_island │ │ └── empty.txt │ ├── p07_the_apocalypse │ │ └── empty.txt │ ├── p04_ants_on_a_triangle │ │ └── empty.txt │ └── p08_the_egg_drop_problem │ │ └── empty.txt ├── chapter07 │ ├── p06_jigsaw │ │ └── empty.txt │ ├── p02_call_center │ │ └── empty.txt │ ├── p03_jukebox │ │ └── empty.txt │ ├── p04_parking_lot │ │ └── empty.txt │ ├── p07_chat_server │ │ └── empty.txt │ ├── p08_othello │ │ └── empty.txt │ ├── p10_minesweeper │ │ └── empty.txt │ ├── p11_file_system │ │ └── empty.txt │ ├── p12_hash_table │ │ └── empty.txt │ ├── p01_deck_of_cards │ │ └── empty.txt │ ├── p09_circular_array │ │ └── empty.txt │ └── p05_online_book_reader │ │ └── empty.txt ├── chapter08 │ ├── p09_parens │ │ └── empty.txt │ ├── p11_coins │ │ └── empty.txt │ ├── p01_triple_step │ │ └── empty.txt │ ├── p03_magic_index │ │ └── empty.txt │ ├── p04_power_set │ │ └── empty.txt │ ├── p10_paint_fill │ │ └── empty.txt │ ├── p13_stack_boxes │ │ └── empty.txt │ ├── p02_robot_in_a_grid │ │ └── empty.txt │ ├── p06_towers_of_hanoi │ │ └── empty.txt │ ├── p12_eight_queens │ │ └── empty.txt │ ├── p05_recursive_multiply │ │ └── empty.txt │ ├── p07_permutations_without_dups │ │ └── empty.txt │ └── p08_permutations_with_dups │ │ └── empty.txt ├── chapter09 │ ├── p05_cache │ │ └── empty.txt │ ├── p01_stock_data │ │ └── empty.txt │ ├── p03_web_crawler │ │ └── empty.txt │ ├── p06_sales_rank │ │ └── empty.txt │ ├── p08_paste_bin │ │ └── empty.txt │ ├── p02_social_network │ │ └── empty.txt │ ├── p04_duplicate_urls │ │ └── empty.txt │ └── p07_personal_financial_manager │ │ └── empty.txt ├── chapter02 │ ├── p01_remove_dups │ │ └── empty.txt │ ├── p04_partition │ │ └── empty.txt │ ├── p05_sum_lists │ │ └── empty.txt │ ├── p06_palindrome │ │ └── empty.txt │ ├── p07_intersection │ │ └── empty.txt │ ├── p08_loop_detection │ │ └── empty.txt │ ├── p02_return_kth_to_last │ │ └── empty.txt │ └── p03_delete_middle_node │ │ └── empty.txt ├── chapter03 │ ├── p02_stack_min │ │ └── empty.txt │ ├── p05_sort_stack │ │ └── empty.txt │ ├── p01_three_in_one │ │ └── empty.txt │ ├── p03_stack_of_plates │ │ └── empty.txt │ ├── p04_queue_via_stacks │ │ └── empty.txt │ └── p06_animal_shelter │ │ └── empty.txt ├── chapter04 │ ├── p06_successor │ │ └── empty.txt │ ├── p07_build_order │ │ └── empty.txt │ ├── p11_random_node │ │ └── empty.txt │ ├── p02_minimal_tree │ │ └── empty.txt │ ├── p03_list_of_depths │ │ └── empty.txt │ ├── p04_check_balanced │ │ └── empty.txt │ ├── p05_validate_bst │ │ └── empty.txt │ ├── p09_bst_sequences │ │ └── empty.txt │ ├── p10_check_subtree │ │ └── empty.txt │ ├── p12_paths_with_sum │ │ └── empty.txt │ ├── p01_route_between_nodes_1 │ │ └── empty.txt │ └── p08_first_common_ancestor │ │ └── empty.txt ├── chapter05 │ ├── p01_insertion │ │ └── empty.txt │ ├── p04_next_number │ │ └── empty.txt │ ├── p05_debugger │ │ └── empty.txt │ ├── p06_conversion │ │ └── empty.txt │ ├── p08_draw_line │ │ └── empty.txt │ ├── p02_binary_to_string │ │ └── empty.txt │ ├── p03_flip_bit_to_win │ │ └── empty.txt │ └── p07_pairwise_swap │ │ └── empty.txt └── chapter10 │ ├── p07_missing_int │ └── empty.txt │ ├── p01_sorted_merge │ └── empty.txt │ ├── p02_group_anagrams │ └── empty.txt │ ├── p05_sparse_search │ └── empty.txt │ ├── p06_sort_big_file │ └── empty.txt │ ├── p08_find_duplicates │ └── empty.txt │ ├── p10_rank_from_stream │ └── empty.txt │ ├── p04_sorted_search_no_size │ └── empty.txt │ ├── p09_sorted_matrix_search │ └── empty.txt │ ├── p11_peaks_and_valleys │ └── empty.txt │ └── p03_search_in_rotated_array │ └── empty.txt ├── Ruby ├── chapter01 │ ├── p01_is_unique │ │ └── empty.txt │ ├── p03_urlify │ │ └── empty.txt │ ├── p04_palinperm │ │ └── empty.txt │ ├── p05_oneaway │ │ └── empty.txt │ ├── p02_check_perm │ │ └── empty.txt │ ├── p07_rotate_matrix │ │ └── empty.txt │ ├── p08_zero_matrix │ │ └── empty.txt │ ├── p06_string_compression │ │ └── empty.txt │ └── p09_string_rotation │ │ └── empty.txt ├── chapter02 │ ├── p04_partition │ │ └── empty.txt │ ├── p05_sum_lists │ │ └── empty.txt │ ├── p01_remove_dups │ │ └── empty.txt │ ├── p06_palindrome │ │ └── empty.txt │ ├── p07_intersection │ │ └── empty.txt │ ├── p08_loop_detection │ │ └── empty.txt │ ├── p02_return_kth_to_last │ │ └── empty.txt │ └── p03_delete_middle_node │ │ └── empty.txt ├── chapter03 │ ├── p02_stack_min │ │ └── empty.txt │ ├── p01_three_in_one │ │ └── empty.txt │ ├── p05_sort_stack │ │ └── empty.txt │ ├── p06_animal_shelter │ │ └── empty.txt │ ├── p03_stack_of_plates │ │ └── empty.txt │ └── p04_queue_via_stacks │ │ └── empty.txt ├── chapter04 │ ├── p06_successor │ │ └── empty.txt │ ├── p02_minimal_tree │ │ └── empty.txt │ ├── p03_list_of_depths │ │ └── empty.txt │ ├── p04_check_balanced │ │ └── empty.txt │ ├── p05_validate_bst │ │ └── empty.txt │ ├── p07_build_order │ │ └── empty.txt │ ├── p09_bst_sequences │ │ └── empty.txt │ ├── p10_check_subtree │ │ └── empty.txt │ ├── p11_random_node │ │ └── empty.txt │ ├── p12_paths_with_sum │ │ └── empty.txt │ ├── p01_route_between_nodes │ │ └── empty.txt │ └── p08_first_common_ancestor │ │ └── empty.txt ├── chapter05 │ ├── p01_insertion │ │ └── empty.txt │ ├── p05_debugger │ │ └── empty.txt │ ├── p08_draw_line │ │ └── empty.txt │ ├── p04_next_number │ │ └── empty.txt │ ├── p06_conversion │ │ └── empty.txt │ ├── p07_pairwise_swap │ │ └── empty.txt │ ├── p02_binary_to_string │ │ └── empty.txt │ └── p03_flip_bit_to_win │ │ └── empty.txt ├── chapter06 │ ├── p03_dominos │ │ └── empty.txt │ ├── p10_poison │ │ └── empty.txt │ ├── p01_heavy_pill │ │ └── empty.txt │ ├── p02_basketball │ │ └── empty.txt │ ├── p05_jugs_of_water │ │ └── empty.txt │ ├── p07_the_apocalypse │ │ └── empty.txt │ ├── p09_100_lockers │ │ └── empty.txt │ ├── p04_ants_on_a_triangle │ │ └── empty.txt │ ├── p06_blue_eyed_island │ │ └── empty.txt │ └── p08_the_egg_drop_problem │ │ └── empty.txt ├── chapter07 │ ├── p03_jukebox │ │ └── empty.txt │ ├── p06_jigsaw │ │ └── empty.txt │ ├── p08_othello │ │ └── empty.txt │ ├── p01_deck_of_cards │ │ └── empty.txt │ ├── p02_call_center │ │ └── empty.txt │ ├── p04_parking_lot │ │ └── empty.txt │ ├── p07_chat_server │ │ └── empty.txt │ ├── p09_circular_array │ │ └── empty.txt │ ├── p10_minesweeper │ │ └── empty.txt │ ├── p11_file_system │ │ └── empty.txt │ ├── p12_hash_table │ │ └── empty.txt │ └── p05_online_book_reader │ │ └── empty.txt ├── chapter08 │ ├── p04_power_set │ │ └── empty.txt │ ├── p09_parens │ │ └── empty.txt │ ├── p11_coins │ │ └── empty.txt │ ├── p01_triple_step │ │ └── empty.txt │ ├── p03_magic_index │ │ └── empty.txt │ ├── p10_paint_fill │ │ └── empty.txt │ ├── p12_eight_queens │ │ └── empty.txt │ ├── p13_stack_boxes │ │ └── empty.txt │ ├── p14_boolean_eval │ │ └── empty.txt │ ├── p02_robot_in_a_grid │ │ └── empty.txt │ ├── p05_recursive_multiply │ │ └── empty.txt │ ├── p06_towers_of_hanoi │ │ └── empty.txt │ ├── p08_permutations_with_dups │ │ └── empty.txt │ └── p07_permutations_without_dups │ │ └── empty.txt ├── chapter09 │ ├── p05_cache │ │ └── empty.txt │ ├── p08_paste_bin │ │ └── empty.txt │ ├── p01_stock_data │ │ └── empty.txt │ ├── p02_social_network │ │ └── empty.txt │ ├── p03_web_crawler │ │ └── empty.txt │ ├── p04_duplicate_urls │ │ └── empty.txt │ ├── p06_sales_rank │ │ └── empty.txt │ └── p07_personal_financial_manager │ │ └── empty.txt └── chapter10 │ ├── p01_sorted_merge │ └── empty.txt │ ├── p02_group_anagrams │ └── empty.txt │ ├── p05_sparse_search │ └── empty.txt │ ├── p06_sort_big_file │ └── empty.txt │ ├── p07_missing_int │ └── empty.txt │ ├── p08_find_duplicates │ └── empty.txt │ ├── p10_rank_from_stream │ └── empty.txt │ ├── p11_peaks_and_valleys │ └── empty.txt │ ├── p03_search_in_rotated_array │ └── empty.txt │ ├── p04_sorted_search_no_size │ └── empty.txt │ └── p09_sorted_matrix_search │ └── empty.txt ├── JavaScript ├── chapter01 │ ├── p05_oneaway │ │ ├── empty.txt │ │ ├── rroque98_sol.js │ │ └── avc278.js │ ├── p04_palinperm │ │ ├── empty.txt │ │ ├── rroque98_sol.js │ │ └── avc278.js │ ├── p07_rotate_matrix │ │ ├── empty.txt │ │ ├── rroque98_sol.js │ │ └── avc278.js │ ├── p08_zero_matrix │ │ ├── empty.txt │ │ ├── rroque98_sol.js │ │ └── avc278.js │ ├── p06_string_compression │ │ ├── empty.txt │ │ ├── rroque98_sol.js │ │ └── avc278.js │ ├── p01_is_unique │ │ ├── fvntr.js │ │ └── avc278.js │ ├── p03_urlify │ │ ├── rroque98_sol.js │ │ ├── averagehuman0G.js │ │ └── avc278.js │ ├── p02_check_perm │ │ ├── YourMobileGeek.js │ │ └── rroque98_sol.js │ └── p09_string_rotation │ │ ├── YourMobileGeek.js │ │ └── avc278.js ├── chapter05 │ ├── p05_debugger │ │ └── empty.txt │ ├── p01_insertion │ │ └── empty.txt │ ├── p04_next_number │ │ └── empty.txt │ ├── p06_conversion │ │ └── empty.txt │ ├── p07_pairwise_swap │ │ └── empty.txt │ ├── p08_draw_line │ │ └── empty.txt │ ├── p02_binary_to_string │ │ └── empty.txt │ └── p03_flip_bit_to_win │ │ └── empty.txt ├── chapter06 │ ├── p03_dominos │ │ └── empty.txt │ ├── p10_poison │ │ └── empty.txt │ ├── p01_heavy_pill │ │ └── empty.txt │ ├── p02_basketball │ │ └── empty.txt │ ├── p05_jugs_of_water │ │ └── empty.txt │ ├── p09_100_lockers │ │ └── empty.txt │ ├── p04_ants_on_a_triangle │ │ └── empty.txt │ ├── p06_blue_eyed_island │ │ └── empty.txt │ ├── p07_the_apocalypse │ │ └── empty.txt │ └── p08_the_egg_drop_problem │ │ └── empty.txt ├── chapter07 │ ├── p03_jukebox │ │ └── empty.txt │ ├── p06_jigsaw │ │ └── empty.txt │ ├── p08_othello │ │ └── empty.txt │ ├── p01_deck_of_cards │ │ └── empty.txt │ ├── p02_call_center │ │ └── empty.txt │ ├── p04_parking_lot │ │ └── empty.txt │ ├── p07_chat_server │ │ └── empty.txt │ ├── p10_minesweeper │ │ └── empty.txt │ ├── p11_file_system │ │ └── empty.txt │ ├── p12_hash_table │ │ └── empty.txt │ ├── p05_online_book_reader │ │ └── empty.txt │ └── p09_circular_array │ │ └── empty.txt ├── chapter08 │ ├── p09_parens │ │ └── empty.txt │ ├── p11_coins │ │ └── empty.txt │ ├── p01_triple_step │ │ └── empty.txt │ ├── p03_magic_index │ │ └── empty.txt │ ├── p04_power_set │ │ └── empty.txt │ ├── p10_paint_fill │ │ └── empty.txt │ ├── p12_eight_queens │ │ └── empty.txt │ ├── p13_stack_boxes │ │ └── empty.txt │ ├── p14_boolean_eval │ │ └── empty.txt │ ├── p02_robot_in_a_grid │ │ └── empty.txt │ ├── p05_recursive_multiply │ │ └── empty.txt │ ├── p06_towers_of_hanoi │ │ └── empty.txt │ ├── p08_permutations_with_dups │ │ └── empty.txt │ └── p07_permutations_without_dups │ │ └── empty.txt ├── chapter09 │ ├── p05_cache │ │ └── empty.txt │ ├── p01_stock_data │ │ └── empty.txt │ ├── p03_web_crawler │ │ └── empty.txt │ ├── p06_sales_rank │ │ └── empty.txt │ ├── p08_paste_bin │ │ └── empty.txt │ ├── p02_social_network │ │ └── empty.txt │ ├── p04_duplicate_urls │ │ └── empty.txt │ └── p07_personal_financial_manager │ │ └── empty.txt ├── chapter02 │ ├── p01_remove_dups │ │ ├── empty.txt │ │ └── avc278.js │ ├── p04_partition │ │ └── empty.txt │ ├── p05_sum_lists │ │ └── empty.txt │ ├── p06_palindrome │ │ ├── empty.txt │ │ └── avc278.js │ ├── p07_intersection │ │ └── empty.txt │ ├── p03_delete_middle_node │ │ ├── empty.txt │ │ └── avc278.js │ ├── p08_loop_detection │ │ └── empty.txt │ └── p02_return_kth_to_last │ │ ├── YourMobileGeek.js │ │ └── avc278.js ├── chapter03 │ ├── p01_three_in_one │ │ └── empty.txt │ ├── p02_stack_min │ │ └── empty.txt │ ├── p05_sort_stack │ │ └── empty.txt │ ├── p03_stack_of_plates │ │ └── empty.txt │ ├── p04_queue_via_stacks │ │ └── empty.txt │ └── p06_animal_shelter │ │ └── empty.txt ├── chapter04 │ ├── p02_minimal_tree │ │ └── empty.txt │ ├── p05_validate_bst │ │ └── empty.txt │ ├── p06_successor │ │ └── empty.txt │ ├── p07_build_order │ │ └── empty.txt │ ├── p09_bst_sequences │ │ └── empty.txt │ ├── p10_check_subtree │ │ └── empty.txt │ ├── p11_random_node │ │ └── empty.txt │ ├── p03_list_of_depths │ │ └── empty.txt │ ├── p04_check_balanced │ │ └── empty.txt │ ├── p12_paths_with_sum │ │ └── empty.txt │ ├── p01_route_between_nodes │ │ └── empty.txt │ └── p08_first_common_ancestor │ │ └── solution.1.js ├── chapter10 │ ├── p01_sorted_merge │ │ └── empty.txt │ ├── p05_sparse_search │ │ └── empty.txt │ ├── p06_sort_big_file │ │ └── empty.txt │ ├── p07_missing_int │ │ └── empty.txt │ ├── p02_group_anagrams │ │ └── empty.txt │ ├── p08_find_duplicates │ │ └── empty.txt │ ├── p10_rank_from_stream │ │ └── empty.txt │ ├── p11_peaks_and_valleys │ │ └── empty.txt │ ├── p03_search_in_rotated_array │ │ └── empty.txt │ ├── p04_sorted_search_no_size │ │ └── empty.txt │ └── p09_sorted_matrix_search │ │ └── empty.txt ├── .eslintrc └── lib │ └── avc278 │ └── linkedlist.js ├── pytest.ini ├── test.sh ├── package.json ├── createDirectories.sh ├── .github └── workflows │ └── ci.yml ├── README.md └── .gitignore /Python/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter03/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p03_urlify/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p10_poison/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p06_jigsaw/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p09_parens/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p11_coins/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter09/p05_cache/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p01_is_unique/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p02_check_perm/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p04_palinperm/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p05_oneaway/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p08_zero_matrix/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter02/p01_remove_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter02/p04_partition/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter02/p05_sum_lists/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter02/p06_palindrome/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter03/p02_stack_min/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter03/p05_sort_stack/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p06_successor/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p07_build_order/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p11_random_node/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter05/p01_insertion/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter05/p04_next_number/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter05/p05_debugger/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter05/p06_conversion/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter05/p08_draw_line/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p01_heavy_pill/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p02_basketball/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p03_dominos/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p09_100_lockers/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p02_call_center/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p03_jukebox/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p04_parking_lot/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p07_chat_server/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p08_othello/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p10_minesweeper/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p11_file_system/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p12_hash_table/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p01_triple_step/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p03_magic_index/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p04_power_set/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p10_paint_fill/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p13_stack_boxes/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter09/p01_stock_data/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter09/p03_web_crawler/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter09/p06_sales_rank/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter09/p08_paste_bin/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p07_missing_int/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p11_coins/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/p05_cache/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p01_is_unique/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p03_urlify/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p04_palinperm/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p05_oneaway/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter02/p04_partition/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter02/p05_sum_lists/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter03/p02_stack_min/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p06_successor/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter05/p01_insertion/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter05/p05_debugger/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter05/p08_draw_line/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p03_dominos/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p10_poison/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p03_jukebox/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p06_jigsaw/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p08_othello/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p04_power_set/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p09_parens/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p11_coins/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter09/p05_cache/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter09/p08_paste_bin/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p07_rotate_matrix/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p09_string_rotation/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter02/p07_intersection/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter02/p08_loop_detection/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter03/p01_three_in_one/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter03/p03_stack_of_plates/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter03/p04_queue_via_stacks/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter03/p06_animal_shelter/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p02_minimal_tree/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p03_list_of_depths/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p04_check_balanced/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p05_validate_bst/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p09_bst_sequences/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p10_check_subtree/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p12_paths_with_sum/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter05/p02_binary_to_string/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter05/p03_flip_bit_to_win/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter05/p07_pairwise_swap/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p05_jugs_of_water/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p06_blue_eyed_island/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p07_the_apocalypse/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p01_deck_of_cards/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p09_circular_array/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p02_robot_in_a_grid/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p06_towers_of_hanoi/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p12_eight_queens/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter09/p02_social_network/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter09/p04_duplicate_urls/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p01_sorted_merge/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p02_group_anagrams/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p05_sparse_search/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p06_sort_big_file/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p08_find_duplicates/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p10_rank_from_stream/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p05_oneaway/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter05/p05_debugger/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p03_dominos/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p10_poison/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p03_jukebox/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p06_jigsaw/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p08_othello/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p09_parens/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p11_coins/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter09/p05_cache/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p01_is_unique/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p02_check_perm/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p03_urlify/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p04_palinperm/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p05_oneaway/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/p04_partition/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/p05_sum_lists/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/p06_palindrome/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter03/p02_stack_min/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter03/p05_sort_stack/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p06_successor/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/p01_insertion/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/p05_debugger/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/p06_conversion/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/p08_draw_line/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p01_heavy_pill/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p02_basketball/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p03_dominos/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p10_poison/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p03_jukebox/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p06_jigsaw/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p08_othello/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p12_hash_table/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p04_power_set/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p09_parens/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p10_paint_fill/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/p01_stock_data/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/p06_sales_rank/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/p08_paste_bin/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p02_check_perm/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p07_rotate_matrix/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p08_zero_matrix/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter02/p01_remove_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter02/p06_palindrome/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter02/p07_intersection/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter02/p08_loop_detection/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter03/p01_three_in_one/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter03/p05_sort_stack/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter03/p06_animal_shelter/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p02_minimal_tree/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p03_list_of_depths/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p04_check_balanced/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p05_validate_bst/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p07_build_order/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p09_bst_sequences/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p10_check_subtree/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p11_random_node/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p12_paths_with_sum/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter05/p04_next_number/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter05/p06_conversion/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter05/p07_pairwise_swap/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p01_heavy_pill/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p02_basketball/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p05_jugs_of_water/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p07_the_apocalypse/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p09_100_lockers/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p01_deck_of_cards/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p02_call_center/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p04_parking_lot/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p07_chat_server/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p09_circular_array/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p10_minesweeper/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p11_file_system/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p12_hash_table/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p01_triple_step/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p03_magic_index/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p10_paint_fill/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p12_eight_queens/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p13_stack_boxes/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p14_boolean_eval/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter09/p01_stock_data/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter09/p02_social_network/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter09/p03_web_crawler/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter09/p04_duplicate_urls/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter09/p06_sales_rank/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p01_sorted_merge/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p02_group_anagrams/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p05_sparse_search/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p06_sort_big_file/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p07_missing_int/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter01/p06_string_compression/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter02/p02_return_kth_to_last/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter02/p03_delete_middle_node/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p01_route_between_nodes_1/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter04/p08_first_common_ancestor/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p04_ants_on_a_triangle/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter06/p08_the_egg_drop_problem/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter07/p05_online_book_reader/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p05_recursive_multiply/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p04_sorted_search_no_size/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p09_sorted_matrix_search/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p11_peaks_and_valleys/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p04_palinperm/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p07_rotate_matrix/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p08_zero_matrix/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p01_remove_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p04_partition/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p05_sum_lists/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p06_palindrome/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p07_intersection/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter03/p01_three_in_one/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter03/p02_stack_min/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter03/p05_sort_stack/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p02_minimal_tree/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p05_validate_bst/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p06_successor/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p07_build_order/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p09_bst_sequences/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p10_check_subtree/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p11_random_node/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter05/p01_insertion/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter05/p04_next_number/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter05/p06_conversion/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter05/p07_pairwise_swap/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter05/p08_draw_line/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p01_heavy_pill/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p02_basketball/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p05_jugs_of_water/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p09_100_lockers/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p01_deck_of_cards/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p02_call_center/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p04_parking_lot/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p07_chat_server/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p10_minesweeper/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p11_file_system/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p12_hash_table/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p01_triple_step/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p03_magic_index/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p04_power_set/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p10_paint_fill/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p12_eight_queens/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p13_stack_boxes/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p14_boolean_eval/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter09/p01_stock_data/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter09/p03_web_crawler/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter09/p06_sales_rank/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter09/p08_paste_bin/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p01_sorted_merge/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p05_sparse_search/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p06_sort_big_file/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p07_missing_int/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p07_rotate_matrix/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p08_zero_matrix/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p09_string_rotation/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/p01_remove_dups/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/p07_intersection/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/p08_loop_detection/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter03/p01_three_in_one/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter03/p03_stack_of_plates/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter03/p06_animal_shelter/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p02_minimal_tree/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p03_list_of_depths/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p04_check_balanced/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p05_validate_bst/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p07_build_order/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p09_bst_sequences/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p10_check_subtree/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p11_random_node/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p12_paths_with_sum/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/p03_flip_bit_to_win/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/p04_next_number/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/p07_pairwise_swap/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p05_jugs_of_water/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p07_the_apocalypse/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p09_100_lockers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p01_deck_of_cards/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p02_call_center/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p04_parking_lot/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p07_chat_server/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p09_circular_array/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p10_minesweeper/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p11_file_system/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p01_triple_step/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p02_robot_in_a_grid/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p03_magic_index/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p06_towers_of_hanoi/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p12_eight_queens/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p13_stack_boxes/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p14_boolean_eval/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/p02_social_network/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/p03_web_crawler/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/p04_duplicate_urls/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p01_sorted_merge/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p02_group_anagrams/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p05_sparse_search/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p06_sort_big_file/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p07_missing_int/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p08_find_duplicates/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p06_string_compression/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter01/p09_string_rotation/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter02/p02_return_kth_to_last/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter02/p03_delete_middle_node/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter03/p03_stack_of_plates/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter03/p04_queue_via_stacks/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p01_route_between_nodes/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter05/p02_binary_to_string/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter05/p03_flip_bit_to_win/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p04_ants_on_a_triangle/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p06_blue_eyed_island/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter07/p05_online_book_reader/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p02_robot_in_a_grid/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p05_recursive_multiply/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p06_towers_of_hanoi/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p08_find_duplicates/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p10_rank_from_stream/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p11_peaks_and_valleys/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | python_files = *.py 3 | -------------------------------------------------------------------------------- /Go/chapter08/p07_permutations_without_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter08/p08_permutations_with_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter09/p07_personal_financial_manager/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Go/chapter10/p03_search_in_rotated_array/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p06_string_compression/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p03_delete_middle_node/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p08_loop_detection/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter03/p03_stack_of_plates/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter03/p04_queue_via_stacks/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter03/p06_animal_shelter/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p03_list_of_depths/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p04_check_balanced/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p12_paths_with_sum/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter05/p02_binary_to_string/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter05/p03_flip_bit_to_win/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p04_ants_on_a_triangle/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p06_blue_eyed_island/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p07_the_apocalypse/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p05_online_book_reader/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter07/p09_circular_array/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p02_robot_in_a_grid/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p05_recursive_multiply/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p06_towers_of_hanoi/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter09/p02_social_network/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter09/p04_duplicate_urls/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p02_group_anagrams/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p08_find_duplicates/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p10_rank_from_stream/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p11_peaks_and_valleys/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter01/p06_string_compression/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/p02_return_kth_to_last/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter02/p03_delete_middle_node/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter03/p04_queue_via_stacks/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p01_route_between_nodes/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter05/p02_binary_to_string/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p04_ants_on_a_triangle/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p06_blue_eyed_island/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter06/p08_the_egg_drop_problem/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter07/p05_online_book_reader/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p05_recursive_multiply/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p09_sorted_matrix_search/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p10_rank_from_stream/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p11_peaks_and_valleys/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter04/p08_first_common_ancestor/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter06/p08_the_egg_drop_problem/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p08_permutations_with_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p03_search_in_rotated_array/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p04_sorted_search_no_size/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter10/p09_sorted_matrix_search/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p01_route_between_nodes/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter06/p08_the_egg_drop_problem/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p08_permutations_with_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p03_search_in_rotated_array/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p04_sorted_search_no_size/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter10/p09_sorted_matrix_search/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter04/p08_first_common_ancestor/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p07_permutations_without_dups/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter08/p08_permutations_with_dups/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p03_search_in_rotated_array/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter10/p04_sorted_search_no_size/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter08/p07_permutations_without_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ruby/chapter09/p07_personal_financial_manager/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter04/p08_first_common_ancestor/solution.1.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter08/p07_permutations_without_dups/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /JavaScript/chapter09/p07_personal_financial_manager/empty.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Python/chapter09/p07_personal_financial_manager/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | yarn test 4 | python3 -m pytest 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "homepage": "https://github.com/techqueria/data-structures-and-algorithms/", 3 | "devDependencies": { 4 | "mocha": "^8.1" 5 | }, 6 | "scripts": { 7 | "test": "mocha --recursive 'JavaScript/**/*.js'" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p01_is_unique/fvntr.js: -------------------------------------------------------------------------------- 1 | /* Write a function isUnique that: 2 | Input: takes an array of integers 3 | Output: returns a deduped array of integers 4 | */ 5 | 6 | // Solution using Set 7 | const isUnique = (arr) => [...new Set(arr)]; 8 | 9 | // Test Cases 10 | const assert = require('assert'); 11 | 12 | describe(module.filename, () => { 13 | it('should deduplicate array', () => { 14 | assert.deepEqual(isUnique([1,1,1,2,2,2,2,3,3,3,3]), [1,2,3]); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /JavaScript/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "quotes": [2, 'single'], 4 | "semi": 2, 5 | "curly": 2, 6 | "camelcase": 0, 7 | "no-unused-vars": 2, 8 | "no-use-before-define": 2, 9 | "no-underscore-dangle": 2, 10 | "strict": 0, 11 | "no-multi-spaces": 2, 12 | "semi-spacing": 2, 13 | "no-shadow": 2 14 | }, 15 | 16 | "env": { 17 | "browser": true, 18 | "node": true, 19 | "mocha": true 20 | }, 21 | "globals": { 22 | "chrome": false 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /createDirectories.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | DIRECTORY="$(git rev-parse --show-toplevel)" 4 | 5 | if [[ $# -lt 1 ]]; then 6 | echo "Usage: bash createDirectories.sh " >&2 7 | exit 1 8 | fi 9 | 10 | programmingLanguage="$1" 11 | rsync --archive --include='*/' --exclude='*' "${DIRECTORY}/Python/" \ 12 | "${DIRECTORY}/${programmingLanguage}/" 13 | 14 | 15 | find "${DIRECTORY}/${programmingLanguage}/" -mindepth 2 -maxdepth 2 \ 16 | -exec touch -- "{}/empty.txt" \; 17 | 18 | git add "${DIRECTORY}/${programmingLanguage}/" -------------------------------------------------------------------------------- /JavaScript/chapter01/p03_urlify/rroque98_sol.js: -------------------------------------------------------------------------------- 1 | // Write a method to replace all spaces within a string with '%20' 2 | 3 | const encodeSpaces = (string) => string.replace(/ /g, '%20'); 4 | 5 | // Tests: 6 | const assert = require('assert'); 7 | 8 | describe(module.filename, () => { 9 | it('should replace spaces with %20', () => { 10 | assert.equal(encodeSpaces('Hello World'), 'Hello%20World'); 11 | assert.equal(encodeSpaces(''), ''); 12 | assert.equal(encodeSpaces('This is an example'), 'This%20is%20an%20example'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p02_check_perm/YourMobileGeek.js: -------------------------------------------------------------------------------- 1 | /* Given two strings, write a method to decide 2 | if one is permutation of the other */ 3 | 4 | var checkPerm = function(stringOne, stringTwo) { 5 | //if different lengths, return false 6 | if (stringOne.length !== stringTwo.length ) { 7 | return false; 8 | } 9 | //else sort & compare 10 | else { 11 | var sortStringOne = stringOne.split('').sort('').join(''); 12 | var sortStringTwo = stringTwo.split('').sort('').join(''); 13 | return sortStringOne === sortStringTwo; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Python/chapter01/p02_check_perm/lomeli_noe.py: -------------------------------------------------------------------------------- 1 | # Given two strings, write a method to decide if one is a permutation of the other 2 | 3 | import unittest 4 | 5 | def isPermutation(str1, str2): 6 | if len(str1) != len(str2): 7 | return False 8 | 9 | sort1 = sorted(str1) 10 | sort2 = sorted(str2) 11 | 12 | return sort1 == sort2 13 | 14 | 15 | class Test(unittest.TestCase): 16 | def test_perm_check(self): 17 | self.assertTrue(isPermutation("stop", "pots")) 18 | self.assertFalse(isPermutation("stops", "ponds")) 19 | 20 | 21 | if __name__ == '__main__': 22 | unittest.main() 23 | -------------------------------------------------------------------------------- /Python/chapter01/p02_check_perm/ismael_duran.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | 4 | def check_perm(example, example2): 5 | for letter in example: 6 | if letter in example2: 7 | index = example2.index(letter) 8 | example2 = example2[:index] + example2[index+1:] 9 | 10 | if example2 == '': 11 | return True 12 | else: 13 | continue 14 | else: 15 | return False 16 | 17 | 18 | class Test(unittest.TestCase): 19 | def test_perm_check(self): 20 | self.assertFalse(check_perm("hello", "ehloe")) 21 | 22 | 23 | if __name__ == '__main__': 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /Python/chapter01/p03_urlify/ismael_duran.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | 4 | def replaceSpace(stringPas, totalLen): 5 | # Use replace() to search for blank sapces in the string 6 | # and replace with "%20" 7 | newString = stringPas.replace(' ',"%20") 8 | 9 | # Can use the length of string passed (second arg) 10 | # to check the length of new string is not greater 11 | # than original length 12 | if len(newString) > totalLen: 13 | return "Fail" 14 | else: 15 | return newString 16 | 17 | class Test(unittest.TestCase): 18 | def test_perm_check(self): 19 | self.assertEqual(replaceSpace("Hi, how is your ",18), "Fail") 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() 24 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p09_string_rotation/YourMobileGeek.js: -------------------------------------------------------------------------------- 1 | /* Assume you have a method isSubstring which checks if one word is a substring 2 | of another. Given two strings, sl and s2, write code to check if s2 is a rotation of sl using only one 3 | call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat") */ 4 | 5 | var StringRotate = function(string1, string2) { 6 | if (string1.length !== string2.length ){ 7 | return false; 8 | } 9 | return ( string2 + string1 ).includes(string1); // one call of Substring 10 | }; 11 | 12 | //Test 13 | const assert = require('assert'); 14 | 15 | describe(module.filename, () => { 16 | it('should detect rotated substrings', () => { 17 | assert.equal(StringRotate('waterbottle', 'erbottlewat'), true); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Unit tests 2 | 3 | on: 4 | pull_request: {} 5 | push: 6 | branches: 7 | - master 8 | 9 | jobs: 10 | javascript-test: 11 | name: JavaScript Tests 12 | 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - name: Check out code 17 | uses: actions/checkout@v2 18 | 19 | - name: Install JavaScript dependencies 20 | run: npm ci 21 | 22 | - name: Run JavaScript tests 23 | run: npm test 24 | 25 | python-test: 26 | name: Python Tests 27 | 28 | runs-on: ubuntu-latest 29 | 30 | steps: 31 | - name: Check out code 32 | uses: actions/checkout@v2 33 | 34 | - name: Install Python dependencies 35 | run: python3 -m pip install pytest 36 | 37 | - name: Run Python tests 38 | run: python3 -m pytest 39 | -------------------------------------------------------------------------------- /Python/chapter01/p09_string_rotation/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import unittest 3 | 4 | def is_rotation(s1, s2): 5 | '''Assumeyou have a method isSubstringwhich checks if oneword is a substring 6 | of another. Given two strings, sl and s2, write code to check if s2 is a rotation of sl using only one 7 | call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat"). ''' 8 | if len(s1) is len(s2) and len(s1) > 0: 9 | return s2 in s1 + s1 10 | return False 11 | 12 | 13 | class Test(unittest.TestCase): 14 | def test1(self): 15 | self.assertTrue(is_rotation('waterbottle', 'erbottlewat')) 16 | self.assertFalse(is_rotation('watermellon', 'watermellons')) 17 | self.assertFalse(is_rotation('waterbottle', 'bottleaterw')) 18 | 19 | 20 | if __name__ == '__main__': 21 | unittest.main() 22 | -------------------------------------------------------------------------------- /Python/chapter01/p07_rotate_matrix/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Rotate Matrix: Given an image represented by an NxN matrix, where each pixel 3 | in the image is 4 bytes, write a method to rotate the image by 90 degrees. 4 | Can you do this in place? 5 | Hints:#51, #100 6 | 7 | """ 8 | 9 | import unittest 10 | 11 | 12 | def rotate_matrix(matrix): 13 | matrix.reverse()# we frist reverse the matrix then we take its transpose 14 | 15 | m = len(matrix) 16 | n = len(matrix[0]) 17 | for i in range(m): 18 | for j in range(i+1,n): 19 | temp = matrix[i][j] 20 | matrix[i][j] = matrix[j][i] 21 | matrix[j][i] = temp 22 | 23 | 24 | class Test(unittest.TestCase): 25 | def test_rotate_matrix(self): 26 | matrix = [[1,2,3],[4,5,6],[7,8,9]] 27 | rotate_matrix(matrix) 28 | self.assertEqual(matrix, [[7, 4, 1], [8, 5, 2], [9, 6, 3]]) 29 | 30 | 31 | if __name__ =='__main__': 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p03_urlify/averagehuman0G.js: -------------------------------------------------------------------------------- 1 | //my code was not originally this clean, but same concept as solution 2 | function URLify(arr, len) { 3 | let spaces = 0; 4 | for(let i = 0; i < len; i++) { 5 | if(arr[i] === ' ') spaces++; 6 | } 7 | //last index 8 | let index = len + spaces * 2 - 1; 9 | 10 | for(let i = len - 1; i >= 0; i--) { 11 | if(arr[i] === ' ') { 12 | arr[index] = '0'; 13 | arr[index - 1] = '2'; 14 | arr[index - 2] = '%'; 15 | index -= 3 16 | } else { 17 | arr[index] = arr[i]; 18 | index--; 19 | } 20 | } 21 | return arr; 22 | } 23 | 24 | //testing 25 | const assert = require('assert'); 26 | 27 | describe(module.filename, () => { 28 | it('should correctly URLify', () => { 29 | let arr = ['M', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'S', 'm', 'i', 't', 'h', ' ', ' ', ' ', ' ']; 30 | let expected = ['M', 'r', '%', '2', '0', 'J', 'o', 'h', 'n', '%', '2', '0', 'S', 'm', 'i', 't', 'h']; 31 | assert.deepEqual(URLify(arr, 13), expected); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /Python/chapter01/p03_urlify/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | ''' 3 | Module that Write a method to replace all spaces in a string with '%20'. You may assume that the string 4 | has sufficient space at the end to hold the additional characters, and that you are given the "true" 5 | length of the string. (Note: If implementing in Java, please use a character array so that you can 6 | perform this operation in place.) 7 | ''' 8 | 9 | import unittest 10 | 11 | def URLify(string, char): 12 | ''' 13 | Function that replace all spaces in a string with '%20'. 14 | Args: 15 | string (str): a string to modify 16 | ''' 17 | return string[:char + 1].replace(" ", "%20") 18 | 19 | class Test(unittest.TestCase): 20 | def test1(self): 21 | input_string = "Mr Johns Smith " 22 | char = 13 23 | output_string = "Mr%20John%20Smith" 24 | 25 | self.assertTrue(URLify(input_string, 13), output_string) 26 | 27 | if __name__ == '__main__': 28 | unittest.main() 29 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p07_rotate_matrix/rroque98_sol.js: -------------------------------------------------------------------------------- 1 | /*Given an image represented by an N*N matrix where 2 | each pixel in the image is 4 bytes, write a method 3 | to rotate the image by 90 degrees. Can you do this 4 | in place?*/ 5 | 6 | const rotateImage = nestedArr => { 7 | const n = nestedArr.length; 8 | if (n === 0 || n === 1) { 9 | return nestedArr; 10 | } 11 | var rotatedArr = []; 12 | for (let col = 0; col < n; col++) { 13 | let newRow = []; 14 | for (let row = n - 1; row >= 0; row--) { 15 | newRow.push(nestedArr[row][col]); 16 | } 17 | rotatedArr.push(newRow); 18 | } 19 | return rotatedArr; 20 | }; 21 | 22 | // TESTS: 23 | const assert = require('assert'); 24 | 25 | describe(module.filename, () => { 26 | it('should rotate matrices', () => { 27 | assert.deepEqual( 28 | rotateImage([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), 29 | [[7, 4, 1], [8, 5, 2], [9, 6, 3]] 30 | ); 31 | assert.deepEqual(rotateImage([[1]]), [[1]]); 32 | assert.deepEqual(rotateImage([[]]), [[]]); 33 | assert.deepEqual(rotateImage([]), []); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /Python/chapter01/p01_is_unique/ismael_duran.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | # Note that this solution assumes a unique string 4 | # implies a lowercase letter and the uppercase of 5 | # that letter is not considered duplicates 6 | # example: the string "Hather" has an uppercase "H" 7 | # and lowercase "h" yet the string has no duplicate 8 | # "h" or "H" 9 | 10 | 11 | def uniqueChar(passedStr): 12 | 13 | # Will use the built-in function set() which 14 | # eliminates dupplicates 15 | deleteDuplicateIfAny = set(passedStr) 16 | 17 | # If the string had unique characters the string 18 | # length remains the same 19 | # If the string has duplicates then size 20 | if len(deleteDuplicateIfAny) == len(passedStr): 21 | return "Unique" 22 | else: 23 | return "Not Unique" 24 | 25 | 26 | class Test(unittest.TestCase): 27 | def test_uniqueChar(self): 28 | example = "Helloh" 29 | self.assertEqual(uniqueChar(example), "Not Unique") 30 | 31 | 32 | if __name__ == '__main__': 33 | unittest.main() 34 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p04_palinperm/rroque98_sol.js: -------------------------------------------------------------------------------- 1 | // Given a string, write a function to check if it is a permutation of a palindrome 2 | 3 | const isPalindromePermutation = (str) => { 4 | const strNoSpaces = str.replace(/ /g, ''); 5 | var oddCount = 0; 6 | const frequencies = new Map(); 7 | for (let char of strNoSpaces) { 8 | frequencies.set(char, 1 + (frequencies.get(char) || 0)); 9 | } 10 | for (let frequency of frequencies.values()) { 11 | if (frequency % 2 === 0) { 12 | continue; 13 | } 14 | oddCount++; 15 | if (oddCount > 1) { 16 | return false; 17 | } 18 | } 19 | return true; 20 | } 21 | 22 | const assert = require('assert'); 23 | 24 | describe(module.filename, () => { 25 | it('should handle positive cases', () => { 26 | assert.equal(isPalindromePermutation('tact coa'), true); 27 | assert.equal(isPalindromePermutation('tact cooa'), true); 28 | }); 29 | 30 | it('should handle negative cases', () => { 31 | assert.equal(isPalindromePermutation('tacr coa'), false); 32 | assert.equal(isPalindromePermutation('tactr coa'), false); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /Python/chapter01/p01_is_unique/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Is Unique: Implement an algorithm to determine if a string has all unique 3 | characters. What if you cannot use additional data structures? 4 | """ 5 | 6 | import unittest 7 | 8 | #This soluation is O(n^2) 9 | 10 | def isUnique(string_): 11 | for i in range(len(string_)): 12 | i = 0 13 | for j in range(1,len(string_)): 14 | if string_[i] == string_[j]: 15 | return False 16 | return True 17 | 18 | 19 | #let's see if we can go faster 20 | 21 | def isUnique_(string_): 22 | d = {} 23 | for i in string_: 24 | if i in d: 25 | d[i] += 1 26 | 27 | else: 28 | d[i] = 1 29 | for keys,value in d.items(): 30 | if value > 1: 31 | return False 32 | return True 33 | 34 | 35 | class Test(unittest.TestCase): 36 | def test_quadradic(self): 37 | self.assertFalse(isUnique("GeeksforGeeks")) 38 | 39 | def test_linear(self): 40 | self.assertTrue(isUnique_("camilo")) 41 | 42 | 43 | if __name__ == '__main__': 44 | unittest.main() 45 | -------------------------------------------------------------------------------- /Python/chapter01/p06_string_compression/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | 1.6:String Compression: Implement a method to perform basic string compression using 3 | the counts of repeated characters. For example, the string aabcccccaaa would 4 | become a2blc5a3. If the "compressed" string would not become smaller than the 5 | original string, your method should return 6 | the original string. You can assume the string has only uppercase and lowercase 7 | letters (a - z). 8 | """ 9 | 10 | import unittest 11 | 12 | 13 | def string_compression(str1): 14 | count = 1 15 | new_str = "" 16 | for i in range(len(str1)-1): 17 | if(str1[i] == str1[i+1]): 18 | count+=1 19 | else: 20 | new_str += str1[i] + str(count) 21 | count = 1 22 | new_str += str1[i] + str(count) 23 | 24 | if len(new_str) < len(str1): 25 | return new_str 26 | 27 | else: 28 | return str1 29 | 30 | 31 | class Test(unittest.TestCase): 32 | def test1(self): 33 | self.assertEqual(string_compression('aabcccccaaa'), 'a2b1c5a3') 34 | 35 | if __name__ == '__main__': 36 | unittest.main() 37 | -------------------------------------------------------------------------------- /Python/chapter01/p05_oneaway/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import unittest 3 | 4 | 5 | def one_away(s1, s2): 6 | '''Given two strings, write a function to check if 7 | they are one edit (or zero edits) away.''' 8 | if abs(len(s1) - len(s2)) > 1: 9 | return False 10 | 11 | str1 = s1 if len(s1) < len(s2) else s2 12 | str2 = s2 if len(s1) < len(s2) else s1 13 | idx1, idx2 = 0, 0 14 | diff = False 15 | 16 | while idx1 < len(str1) and idx2 < len(str2): 17 | if str1[idx1] is not str2[idx2]: 18 | if diff: 19 | return False 20 | diff = True 21 | 22 | if len(str1) is len(str2): 23 | idx1 += 1 24 | else: 25 | idx1 += 1 26 | idx2 += 1 27 | 28 | return True 29 | 30 | 31 | class Test(unittest.TestCase): 32 | def test1(self): 33 | s1 = "Tact Cat" 34 | s2 = "TactCat" 35 | s3 = "nick" 36 | s4 = "jeff" 37 | self.assertTrue(one_away(s1, s2)) 38 | self.assertFalse(one_away(s3, s4)) 39 | 40 | 41 | if __name__ == '__main__': 42 | unittest.main() 43 | -------------------------------------------------------------------------------- /Python/chapter01/p09_string_rotation/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | String Rotation:Assume you have a method isSubstring which checks if one word is a 3 | substring of another. Given two strings, sl and s2, write code to check if s2 is 4 | a rotation of sl using only one call to isSubstring (e.g.,"waterbottle" is a 5 | rotation of"erbottlewat"). 6 | """ 7 | 8 | import unittest 9 | 10 | 11 | def isSubstring(s1,s2): 12 | if s2 in s1: #check to see if the seqence of s2 is found in s1 13 | return True 14 | return False 15 | 16 | def Rotation(s1,s2): 17 | """ 18 | if they are substring of each other then we first check its lenght 19 | """ 20 | if len(s1) != len(s2): #if there not the same lenght then there different woulds 21 | return False 22 | 23 | else: 24 | s1+=s1 #waterbottlewaterbottle Anyroataion can be found here 25 | return isSubstring(s1,s2) 26 | 27 | 28 | class Test(unittest.TestCase): 29 | def test_string_rotation(self): 30 | s1 = "waterbottle" 31 | s2 = "erbottlewat" 32 | 33 | self.assertTrue(Rotation(s1,s2)) 34 | 35 | 36 | if __name__ =='__main__': 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /Python/chapter01/p01_is_unique/lomeli_noe.py: -------------------------------------------------------------------------------- 1 | # Implement an algorithm to determine if a string has all unique charactersself. 2 | # What if you cannot use additional data structures. 3 | 4 | # For this problem I will assume we are working ASCII characters for which there 5 | # are 128 unique values. 6 | 7 | import array 8 | import unittest 9 | 10 | def isUnique(str): 11 | 12 | # if the string is longer than 128 characters, it means there are duplicates 13 | if len(str) > 128: 14 | return false 15 | 16 | # create an array to map the values 17 | mask = array.array('i',(0,)*128) 18 | 19 | for i in str: 20 | # the first time we find a character we change the value at that index 21 | # from 0 to 1 22 | if mask[ord(i)] == 0: 23 | mask[ord(i)] = 1 24 | # if the value at that index is already 1, it means this is a duplicate 25 | else: 26 | return False 27 | return True 28 | 29 | 30 | class Test(unittest.TestCase): 31 | def test_is_unique(self): 32 | self.assertFalse(isUnique("test")) 33 | self.assertTrue(isUnique("abc123")) 34 | 35 | 36 | if __name__ == '__main__': 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /Python/chapter01/p03_urlify/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | URLify: Write a method to replace all spaces in a string with '%20'. You may 3 | assume that the string has sufficient space at the end to hold the additional 4 | characters,and that you are given the "true" length of the string. 5 | (Note: If implementing in Java,please use a character array so that you can 6 | perform this operation in place.) 7 | EXAMPLE 8 | Input: "Mr John Smith ", 13 Output: "Mr%20John%20Smith" 9 | """ 10 | 11 | import unittest 12 | 13 | 14 | def urlify(str): 15 | len_ = len(str) #gets the lenght of str 16 | new_str = ""#build a new string 17 | for i in range(len_-1): 18 | if str[i] == " " and str[i+1]==" ":#if two space aheah are space then break 19 | break 20 | if str[i] == " ": #if we find a space just add %20 21 | new_str+= "%20" 22 | 23 | else: #just add the chars 24 | new_str+= str[i] 25 | 26 | return new_str 27 | 28 | class Test(unittest.TestCase): 29 | def test_perm_check(self): 30 | self.assertEqual(urlify("MY house is on fire "), 31 | "MY%20house%20is%20on%20fire") 32 | 33 | 34 | if __name__ == '__main__': 35 | unittest.main() 36 | -------------------------------------------------------------------------------- /JavaScript/lib/avc278/linkedlist.js: -------------------------------------------------------------------------------- 1 | const assert = require("assert"); 2 | 3 | class LinkedListNode { 4 | constructor(val, next) { 5 | this.val = val === undefined ? null : val; 6 | this.next = next === undefined ? null : next; 7 | } 8 | } 9 | 10 | const arrayToLinkedList = (arr) => { 11 | let tail = null; 12 | for (let i = arr.length - 1; i >= 0; i--) { 13 | tail = new LinkedListNode(arr[i], tail); 14 | } 15 | return tail; 16 | }; 17 | 18 | const compareLinkedLists = (A, B) => { 19 | if (!A && !B) return true; 20 | 21 | while (A !== null && B !== null) { 22 | if (A.val !== B.val) return false; 23 | A = A.next; 24 | B = B.next; 25 | } 26 | 27 | return !A && !B; 28 | }; 29 | 30 | describe(module.filename, () => { 31 | it("should return false when comparing unequal linked lists", () => { 32 | assert.ok( 33 | !compareLinkedLists(arrayToLinkedList([]), arrayToLinkedList(["hi!"])) 34 | ); 35 | }); 36 | it("should return false when comparing another pair of unequal linked lists", () => { 37 | assert.ok( 38 | !compareLinkedLists(arrayToLinkedList([1, 2]), arrayToLinkedList([1])) 39 | ); 40 | }); 41 | }); 42 | 43 | module.exports = { arrayToLinkedList, compareLinkedLists, LinkedListNode }; 44 | -------------------------------------------------------------------------------- /Python/chapter01/p02_check_perm/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Check Permutation: Given two strings,write a method to decide if 3 | one is a permutation of the other. 4 | """ 5 | 6 | import unittest 7 | 8 | 9 | #O(n) sol? 10 | def perm_check(str_1,str_2): 11 | if len(str_1) != len(str_2):#If both str are diiferent lenghts return false 12 | return False 13 | d = {} 14 | for i in str_1: #takes all the letter in string one and add them to the hash map 15 | if i in d: #counts the number of letter 16 | d[i] += 1 17 | else: 18 | d[i] = 1 19 | 20 | for j in str_2: # 21 | if j in d:#Looks at the letter in the string and subtract the number 22 | #of the same letter from the value 23 | d[j] -= 1 24 | else: 25 | d[j] = 1 26 | 27 | for keys,value in d.items(): 28 | if value > 0: 29 | return False 30 | return True 31 | 32 | 33 | class Test(unittest.TestCase): 34 | def test_perm_check(self): 35 | self.assertFalse(perm_check("camilo","pop")) 36 | self.assertFalse(perm_check("camilo","camplo")) 37 | self.assertTrue(perm_check("camilo","olimac")) 38 | 39 | 40 | if __name__ == '__main__': 41 | unittest.main() 42 | -------------------------------------------------------------------------------- /Python/chapter01/p04_palinperm/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Palindrome Permutation: Given a string, write a function to check if it is a 3 | permutation of a palin­ drome. A palindrome is a word or phrase that is the same 4 | forwards and backwards. A permutation is a rearrangement of letters. 5 | The palindrome does not need to be limited to just dictionary words. 6 | EXAMPLE 7 | Input: Tact Coa 8 | Output: True (permutations: "taco cat", "atco eta", etc.) 9 | """ 10 | 11 | import unittest 12 | 13 | 14 | def PalinPerm(str): 15 | d = {} 16 | str = str.replace(' ','').lower()#Take out the spaces 17 | for i in str: 18 | if i in d: 19 | d[i] += 1 20 | else: 21 | d[i] = 1 22 | odd_count = 0 # pop ppo opp 23 | for keys,value in d.items(): 24 | if value % 2 != 0 and odd_count == 0:# Remeber there should only be 1 where key equals when val%2 25 | odd_count += 1 26 | elif value % 2 != 0 and odd_count != 0: 27 | return False 28 | return True 29 | 30 | class Test(unittest.TestCase): 31 | def test_palin_perm(self): 32 | self.assertTrue(PalinPerm('taco cat')) 33 | self.assertFalse(PalinPerm('taco catt')) 34 | 35 | 36 | if __name__ == '__main__': 37 | unittest.main() 38 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p02_check_perm/rroque98_sol.js: -------------------------------------------------------------------------------- 1 | /*Given two strings, write a method to decide 2 | if one is apermutation of the other permutation:*/ 3 | 4 | const isPermutation = (str1, str2) => { 5 | if (str1.length !== str2.length) { 6 | return false; 7 | } 8 | const freq1 = determineCharCount(str1); 9 | const freq2 = determineCharCount(str2); 10 | for (const [char, count] of freq1) { 11 | if (count !== freq2.get(char)) { 12 | return false; 13 | } 14 | } 15 | return true; 16 | //******** helper function ****** 17 | function determineCharCount(string) { 18 | return string 19 | .split('') 20 | .reduce( 21 | (acc, char) => acc.set(char, (acc.get(char) || 0) + 1), 22 | new Map() 23 | ); 24 | } 25 | }; 26 | 27 | // Tests: 28 | const assert = require('assert'); 29 | 30 | describe(module.filename, () => { 31 | it('should handle positive cases', () => { 32 | assert.equal(isPermutation('abc', 'bac'), true); 33 | assert.equal(isPermutation('', ''), true); 34 | assert.equal(isPermutation('12', '21'), true); 35 | }); 36 | 37 | it('should handle negative cases', () => { 38 | assert.equal(isPermutation('abc', 'abb'), false); 39 | assert.equal(isPermutation('abb', 'abc'), false); 40 | assert.equal(isPermutation('aaa', 'abc'), false); 41 | assert.equal(isPermutation('abc', 'abcd'), false); 42 | }); 43 | }); 44 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p09_string_rotation/avc278.js: -------------------------------------------------------------------------------- 1 | // String Rotation: Assume you have a method `isSubstring` which checks if one word is a substring of another. Given two 2 | // strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to `isSubstring`. 3 | 4 | const assert = require("assert"); 5 | 6 | /** 7 | * Checks if one string is a rotation of another string 8 | * @param {string} s1 input string to check against 9 | * @param {string} s2 input string to check if is a rotation of s1 10 | * @return {boolean} whether s2 is a rotation of s1 11 | * 12 | * We can use the built-in method String.prototype.includes() to solve the problem for us, 13 | * checking if s1s1 of size 2N, where N is the length of s1, contains the string s2, also of size N at this point. 14 | * 15 | * Runtime: O(N) 16 | * Space: O(1) 17 | * 18 | */ 19 | const isSubstring = (s1, s2) => { 20 | if (s1.length !== s2.length) return false; 21 | return `${s1}${s1}`.includes(s2); 22 | }; 23 | 24 | describe(module.filename, () => { 25 | it("should return false when both input strings are not of the same length", () => { 26 | const s1 = "hi"; 27 | const s2 = "hello"; 28 | assert.ok(!isSubstring(s1, s2)); 29 | }); 30 | it("should return true when s2 is a valid rotation of s1", () => { 31 | const s1 = "waterbottle"; 32 | const s2 = "erbottlewat"; 33 | assert.ok(isSubstring(s1, s2)); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p02_return_kth_to_last/YourMobileGeek.js: -------------------------------------------------------------------------------- 1 | /* Return Kth to Last: Implement an algorithm to find the kth to last element of a singly linked list. */ 2 | 3 | var linkedList = function(value) { 4 | this.value = value; 5 | this.next = null; 6 | }; 7 | 8 | // N.B. this algorithm is wrong, since it does not produce the desired result. 9 | var findKthToLast = function(k, head) { 10 | //do recursive 11 | if ( head == null || k < 1 ) { 12 | return null; 13 | } else if ( k == 1 ) { 14 | return head.value; 15 | } else { 16 | return findKthToLast(k-1, head.next); 17 | } 18 | }; 19 | 20 | /* Tests */ 21 | const assert = require('assert'); 22 | 23 | describe(module.filename, () => { 24 | it('should find k-th to last entry', () => { 25 | var a = new linkedList('1'); 26 | var b = new linkedList('2'); 27 | var c = new linkedList('3'); 28 | var d = new linkedList('4'); 29 | var e = new linkedList('5'); 30 | var f = new linkedList('6'); 31 | var g = new linkedList('7'); 32 | 33 | a.next = b; 34 | b.next = c; 35 | c.next = d; 36 | d.next = e; 37 | e.next = f; 38 | f.next = g; 39 | 40 | assert.equal(findKthToLast(3, a), '3'); 41 | assert.equal(findKthToLast(10, a), null); 42 | assert.equal(findKthToLast(-1, a), null); 43 | assert.equal(findKthToLast(0, a), null); 44 | assert.equal(findKthToLast(1, a), '1'); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p06_string_compression/rroque98_sol.js: -------------------------------------------------------------------------------- 1 | /* Implement a method to perform basic string 2 | compression using the counts of repeated characters. 3 | If the compressed string length is more than original 4 | string length, return original string. 5 | Ex: 'aabcccccaaa' would become a2b1c5a3 6 | */ 7 | 8 | const stringCompression = (str) => { 9 | if (!str.length) { 10 | return ''; 11 | } 12 | let compStr = ''; 13 | let count = 0; 14 | let currentChar = str[0]; 15 | for (let i = 0; i < str.length; i++) { 16 | let char = str[i]; 17 | if (char === currentChar) { 18 | count++; 19 | } else { 20 | compStr += `${currentChar}${count}`; 21 | currentChar = char; 22 | count = 1; 23 | } 24 | } 25 | compStr += `${currentChar}${count}`; 26 | if (compStr.length > str.length) { 27 | return str; 28 | } 29 | return compStr; 30 | } 31 | 32 | const assert = require('assert'); 33 | 34 | describe(module.filename, () => { 35 | it('should correctly compress longer strings', () => { 36 | assert.equal(stringCompression('aabcccccaaa'), 'a2b1c5a3'); 37 | assert.equal(stringCompression('cccccccc'), 'c8'); 38 | }); 39 | 40 | it('should leave uncompressible strings unmodified', () => { 41 | assert.equal(stringCompression(''), ''); 42 | // Explanation: 'A1a1b1c2C1c2a3' length is longer than original string so returns original string 43 | assert.equal(stringCompression('AabccCccaaa'), 'AabccCccaaa'); 44 | assert.equal(stringCompression('x'), 'x'); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /Python/chapter03/p01_three_in_one/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | class stack: 2 | def __init__(self): 3 | self.arr = [] 4 | 5 | def push(self,item): 6 | """ 7 | Add an item to the top of the stack. 8 | """ 9 | self.arr.insert(0,item) 10 | 11 | def pop_(): 12 | """ 13 | Remove the top item from the stack. 14 | """ 15 | arr = self.arr 16 | len_ = len(arr) 17 | return self.arr.pop(arr) 18 | 19 | 20 | def peek(self): 21 | """ 22 | Return the top of the stack. 23 | """ 24 | return self.arr[0] 25 | 26 | def isEmpty(): 27 | """ 28 | Return true if and only if the stack is empty. 29 | """ 30 | if self.arr == None: 31 | return True 32 | return False 33 | 34 | 35 | 36 | class queue: 37 | def __init__(self): 38 | self.arr = [] 39 | 40 | def add(self,item): 41 | """ 42 | Add an item to the end of the list. 43 | """ 44 | self.arr.append(item) 45 | 46 | 47 | def remove(self): 48 | """ 49 | Remove the first item in the list. 50 | """ 51 | return self.arr.pop(0) 52 | 53 | def peek(self): 54 | """ 55 | Return the top of the queue. 56 | """ 57 | return self.arr[0] 58 | 59 | def isEmpty(): 60 | """ 61 | Return true if and only if the queue is empty. 62 | """ 63 | if self.arr == None: 64 | return True 65 | return False 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # data-and-algorithms ![Travis build status](https://travis-ci.org/techqueria/data-structures-and-algorithms.svg?branch=master) 2 | 3 | Let's go over data structures and algorithms together! 4 | 5 | We will use [Cracking the Coding Interview Book](http://www.crackingthecodinginterview.com/) as reference. 6 | 7 | Feel free to add your solutions in your programming language of your choice 😃 8 | 9 | 10 | Run the following script to create the directory structure for your programming language 11 | if one is not already created 12 | 13 | ```shell 14 | bash createDirectories.sh 15 | ``` 16 | 17 | ## Contributing 18 | --- 19 | 20 | > To get started... 21 | 22 | 1. 🍴 [Fork](https://github.com/techqueria/data-and-algorithms#fork-destination-box) this repository 23 | 24 | 2. 🔨 Commit your changes to your forked repo 25 | 26 | 3. 🎉 Open a new [pull request](https://github.com/techqueria/data-and-algorithms/compare) with changes from your forked repo and get it approved! 27 | * To pull down the most recent changes on this repo, make this branch your upstream 28 | 29 | Check out [Noé Lomelí](https://github.com/noelomeli)'s video for [visual directions](https://www.youtube.com/watch?v=sv97x3yPbrw) 30 | 31 | 32 | ## Join Slack Channel 33 | --- 34 | 35 | Request an invite here: [techqueria.org/slack](https://techqueria.org/communities/slack/) 36 | 37 | Follow along with the community on the `#data-and-algorithms` channel on our Slack 38 | 39 | Make sure to also check out our `#python` `#ruby` `#javascript` and `#golang` channels while you're there 40 | -------------------------------------------------------------------------------- /Python/chapter01/p08_zero_matrix/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import random 3 | import unittest 4 | 5 | 6 | def build_matrix(w, h, max=10): 7 | '''function that builds a matrix with random numbers including 0''' 8 | return [[random.randint(0, max) for _ in range(w)] for _ in range(h)] 9 | 10 | 11 | def zero_matrix(matrix): 12 | '''Write an algorithm such that if an element in an MxN matrix is 0, its entire row and 13 | column are set to 0.''' 14 | row, column = [], [] 15 | zero_present = False 16 | 17 | for idx1 in range(len(matrix)): 18 | for idx2 in range(len(matrix[idx1])): 19 | if matrix[idx1][idx2] == 0: 20 | zero_present = True 21 | row.append(idx1) 22 | column.append(idx2) 23 | 24 | if zero_present: 25 | for idx1 in range(len(matrix)): 26 | for idx2 in range(len(matrix[idx1])): 27 | if idx1 in row: 28 | matrix[idx1][idx2] = 0 29 | if idx2 in column: 30 | matrix[idx1][idx2] = 0 31 | 32 | class Test(unittest.TestCase): 33 | def test_rotate_matrix(self): 34 | matrix = [[9, 5, 7, 7], 35 | [10, 4, 0, 10], 36 | [1, 7, 3, 7], 37 | [6, 8, 4, 4]] 38 | zero_matrix(matrix) 39 | self.assertEqual( 40 | matrix, 41 | [[9, 5, 0, 7], 42 | [0, 0, 0, 0], 43 | [1, 7, 0, 7], 44 | [6, 8, 0, 4]]) 45 | 46 | 47 | if __name__ =='__main__': 48 | unittest.main() 49 | -------------------------------------------------------------------------------- /Python/chapter03/p02_stack_min/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Stack Min: How would you design a stack which, in addition to push and pop, has 3 | a function min which returns the minimum element? Push, pop and min should all 4 | operate in 0(1) time. 5 | Hints:#27, #59, #78 6 | """ 7 | 8 | import unittest 9 | 10 | 11 | class stack: 12 | def __init__(self): 13 | self.items = [] 14 | 15 | def push(self,item): 16 | """ 17 | Add an item to the top of the stack. 18 | """ 19 | self.items.append(item) 20 | 21 | def pop_(self): 22 | """ 23 | Remove the top item from the stack. 24 | """ 25 | 26 | return self.items.pop() 27 | 28 | 29 | def peek(self): 30 | """ 31 | Return the top of the stack. 32 | """ 33 | return self.items[len(self.items)-1] 34 | 35 | def isEmpty(self): 36 | """ 37 | Return true if and only if the stack is empty. 38 | """ 39 | if self.items == None: 40 | return True 41 | return False 42 | 43 | def print_stack(self): 44 | print(self.items) 45 | 46 | def stackMin(self): 47 | arr = self.items 48 | min = arr[0] 49 | for i in range(len(arr)): 50 | if min > arr[i]: 51 | min = arr[i] 52 | 53 | return min 54 | 55 | 56 | class Test(unittest.TestCase): 57 | def test_stack_min(self): 58 | st1 = stack() 59 | st1.push(7) 60 | st1.push(4) 61 | st1.push(5) 62 | st1.push(9) 63 | st1.push(0) 64 | st1.push(6) 65 | st1.push(1) 66 | st1.push(5) 67 | st1.push(7) 68 | self.assertEqual(st1.stackMin(), 0) 69 | 70 | if __name__ == '__main__': 71 | unittest.main() 72 | -------------------------------------------------------------------------------- /Python/chapter01/p06_string_compression/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import unittest 3 | 4 | 5 | def string_compression(string): 6 | '''Implement a method to perform basic string compression using the counts 7 | of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the 8 | "compressed" string would not become smaller than the original string, your method should return 9 | the original string. You can assume the string has only uppercase and lowercase letters (a - z). ''' 10 | if not isinstance(string, str): 11 | return False 12 | 13 | new_str = '' 14 | count, new_count = 0, 0 15 | for idx in range(len(string) - 1): 16 | count += 1 17 | if string[idx] is not string[idx + 1]: 18 | new_str += string[idx] 19 | new_str += str(count) 20 | count = 0 21 | if idx is len(string) - 2: 22 | if string[idx] is string[idx + 1]: 23 | new_str += string[idx] 24 | new_str += str(count + 1) 25 | else: 26 | new_str += string[idx + 1] 27 | new_str += '1' 28 | 29 | for letter in new_str: 30 | if letter == '1': 31 | new_count += 1 32 | 33 | if len(string) // new_count == 1: 34 | return string 35 | return new_str 36 | 37 | 38 | class Test(unittest.TestCase): 39 | def test1(self): 40 | self.assertEqual(string_compression('aabcccccaaa'), 'a2b1c5a3') 41 | self.assertEqual( 42 | string_compression('bbaaaabcdeeefff'), 43 | 'b2a4b1c1d1e3f3') 44 | self.assertEqual(string_compression('abcdef'), 'abcdef') 45 | self.assertNotEqual(string_compression('abcdef'), 'a1b1c1d1e1f1') 46 | 47 | 48 | if __name__ == '__main__': 49 | unittest.main() 50 | -------------------------------------------------------------------------------- /Python/chapter01/p02_check_perm/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | ''' 3 | Module that Given two strings, write a method to decide if one is a permutation of the 4 | other. 5 | 6 | ''' 7 | 8 | import unittest 9 | 10 | def permutation(string1, string2): 11 | ''' 12 | Checks two strings if one is a permutation of the other 13 | Args: 14 | string1 (str): a string 15 | string2 (str): another string 16 | ''' 17 | if len(string1) is not len(string2): 18 | return False 19 | 20 | n1, n2 = sorted(string1), sorted(string2) 21 | return n1 == n2 22 | 23 | def permutation2(string1, string2): 24 | ''' 25 | Checks two strings if one is a permutation of the other 26 | Args: 27 | string1 (str): a string 28 | string2 (str): another string 29 | ''' 30 | if len(string1) is not len(string2): 31 | return False 32 | 33 | d1, d2 = {}, {} 34 | for idx in range(len(string1)): 35 | d1[string1[idx]] = d1.get(string1[idx], 0) + 1 36 | d2[string2[idx]] = d2.get(string2[idx], 0) + 1 37 | 38 | return d1 == d2 39 | 40 | def permutation3(string1, string2): 41 | ''' 42 | Checks two strings if one is a permutation of the other 43 | Args: 44 | string1 (str): a string 45 | string2 (str): another string 46 | ''' 47 | 48 | 49 | class Test(unittest.TestCase): 50 | def test1(self): 51 | t1 = "nickiiii" 52 | t2 = "iiiiinck" 53 | x1, x2 = "hello", "world" 54 | self.assertTrue(permutation(t1,t2)) 55 | self.assertFalse(permutation(x1, x2)) 56 | 57 | def test2(self): 58 | self.assertFalse(permutation2("nickjjj", "nickkki")) 59 | self.assertTrue(permutation2("iiiiinn", "nniiiii")) 60 | 61 | def test3(self): 62 | pass 63 | 64 | if __name__ == '__main__': 65 | unittest.main() 66 | -------------------------------------------------------------------------------- /Python/chapter02/p06_palindrome/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import logging 4 | import unittest 5 | 6 | 7 | class Node: 8 | def __init__(self, data, next=None): 9 | self.data = data 10 | self.next = next 11 | 12 | 13 | class SinglyLinkedList: 14 | def __init__(self): 15 | self.head = None 16 | 17 | def appendNode(self, data): 18 | node = Node(data) 19 | if not self.head: 20 | self.head = node 21 | else: 22 | temp = self.head 23 | while temp.next: 24 | temp = temp.next 25 | temp.next = node 26 | logging.debug('Node added to LL--> %r', data) 27 | 28 | def isPalindrome(self): 29 | if not self.head: 30 | logging.debug('No LL to traverse') 31 | return 32 | 33 | pali = [] 34 | temp = self.head 35 | while temp: 36 | pali.append(temp.data) 37 | temp = temp.next 38 | 39 | strpali = ''.join(pali) 40 | return strpali == strpali[::-1] 41 | 42 | 43 | class Test(unittest.TestCase): 44 | @classmethod 45 | def setUpClass(cls): 46 | cls.l = SinglyLinkedList() 47 | cls.l.appendNode('r') 48 | cls.l.appendNode('a') 49 | cls.l.appendNode('c') 50 | cls.l.appendNode('e') 51 | cls.l.appendNode('c') 52 | cls.l.appendNode('a') 53 | cls.l.appendNode('r') 54 | 55 | cls.k = SinglyLinkedList() 56 | cls.k.appendNode('n') 57 | cls.k.appendNode('i') 58 | cls.k.appendNode('c') 59 | cls.k.appendNode('k') 60 | 61 | @classmethod 62 | def tearDownClass(cls): 63 | pass 64 | 65 | def test1(self): 66 | self.assertTrue(self.l.isPalindrome()) 67 | self.assertFalse(self.k.isPalindrome()) 68 | 69 | 70 | if __name__ == '__main__': 71 | unittest.main() 72 | -------------------------------------------------------------------------------- /Python/chapter01/p08_zero_matrix/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, 3 | its entire row and column are set to 0. 4 | Hints:#17, #74, #702 5 | 6 | 7 | 8 | I've been stuck on this for a couple of days.. 9 | Really good resource: 10 | https://www.youtube.com/watch?v=ZzpJgRvqSJQ&t=113s 11 | https://www.youtube.com/watch?v=qWeNXOCff3o&t=408s 12 | """ 13 | 14 | import unittest 15 | 16 | 17 | def zeroMatrix(matrix): 18 | rowZero = False 19 | colZero = False 20 | 21 | for i in range(0,len(matrix)): 22 | if matrix[i][0] == 0: 23 | colZero = True 24 | 25 | for i in range(0,len(matrix[0])): 26 | if matrix[0][i] == 0: 27 | rowZero = True 28 | 29 | for i in range(1,len(matrix)): 30 | for j in range(1,len(matrix[0])): 31 | if matrix[i][j] == 0: 32 | matrix[0][j] == 0 33 | matrix[i][0] == 0 34 | 35 | 36 | for i in range(1,len(matrix)): 37 | if(matrix[i][0] == 0): 38 | for j in range(1,len(matrix[0])): 39 | matrix[i][j] = 0 40 | 41 | for i in range(1,len(matrix[0])): 42 | if(matrix[0][i] == 0): 43 | for j in range(1,len(matrix)): 44 | matrix[i][j] = 0 45 | 46 | if rowZero: 47 | for i in range(0,len(matrix[0])): 48 | matrix[0][i] = 0 49 | 50 | if colZero: 51 | for i in range(0,len(matrix)): 52 | matrix[i][0] = 0 53 | 54 | 55 | class Test(unittest.TestCase): 56 | def test_rotate_matrix(self): 57 | matrix = [[0,1,1], 58 | [1,1,1], 59 | [1,1,1]] 60 | zeroMatrix(matrix) 61 | self.assertEqual(matrix, 62 | [[0,0,0], 63 | [0,1,1], 64 | [0,1,1]]) 65 | 66 | 67 | if __name__ =='__main__': 68 | unittest.main() 69 | -------------------------------------------------------------------------------- /Python/chapter03/p02_stack_min/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import logging 4 | import unittest 5 | 6 | 7 | class Stack: 8 | def __init__(self): 9 | self.stack = [] 10 | self.min = None 11 | 12 | def getMin(self): 13 | return self.min 14 | 15 | def pop(self): 16 | if not self.length: 17 | logging.debug('No stack to pop') 18 | return None 19 | 20 | item = self.stack.pop() 21 | logging.debug('Item popped %r', item) 22 | 23 | if item == self.min: 24 | if self.length() > 0: 25 | self.findMin() 26 | else: 27 | self.min = None 28 | return item 29 | 30 | def findMin(self): 31 | self.min = self.stack[0] 32 | for x in self.stack: 33 | if x < self.min: 34 | self.min = x 35 | 36 | def length(self): 37 | return len(self.stack) 38 | 39 | def push(self, value): 40 | if not self.min: 41 | self.min = value 42 | elif value < self.min: 43 | self.min = value 44 | 45 | self.stack.append(value) 46 | logging.debug('Item added to stack %r', value) 47 | 48 | def peek(self): 49 | if not self.length: 50 | logging.debug('No stack to peek') 51 | return 52 | logging.debug('Peeking into stack %r', self.stack[-1]) 53 | 54 | 55 | class Test(unittest.TestCase): 56 | @classmethod 57 | def setUpClass(cls): 58 | pass 59 | 60 | @classmethod 61 | def tearDownClass(cls): 62 | pass 63 | 64 | def test1(self): 65 | arr = [2, 5, 7, 10, 3, 10, -1] 66 | s = Stack() 67 | for x in arr: 68 | s.push(x) 69 | self.assertEqual(s.getMin(), -1) 70 | self.assertEqual(s.pop(), -1) 71 | self.assertEqual(s.getMin(), 2) 72 | 73 | 74 | if __name__ == '__main__': 75 | unittest.main() 76 | -------------------------------------------------------------------------------- /Python/chapter03/p05_sort_stack/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | '''Queue via Stacks: Implement a MyQueue class 3 | which implements a queue using two stacks.''' 4 | 5 | import logging 6 | import unittest 7 | 8 | 9 | class Stack: 10 | def __init__(self): 11 | self.stack = [] 12 | 13 | def pop(self): 14 | if self.isEmpty(): 15 | logging.debug('No stack to pop') 16 | return 17 | 18 | return self.stack.pop() 19 | 20 | def isEmpty(self): 21 | return len(self.stack) == 0 22 | 23 | def push(self, value): 24 | self.stack.append(value) 25 | 26 | def peek(self): 27 | if self.isEmpty(): 28 | logging.debug('No stack to peek') 29 | return 30 | return self.stack[-1] 31 | 32 | def getStack(self): 33 | if self.isEmpty(): 34 | logging.debug('No stack to peeek') 35 | return [] 36 | 37 | return self.stack[::-1] 38 | 39 | def sort(self): 40 | r = Stack() 41 | while not self.isEmpty(): 42 | temp = self.pop() 43 | logging.debug('Temp -> %r', temp) 44 | while not r.isEmpty() and r.peek() > temp: 45 | self.push(r.pop()) 46 | r.push(temp) 47 | logging.debug('self stack %r', self.stack) 48 | logging.debug('%r', r.stack) 49 | 50 | while not r.isEmpty(): 51 | self.push(r.pop()) 52 | 53 | 54 | class Test(unittest.TestCase): 55 | @classmethod 56 | def setUpClass(cls): 57 | pass 58 | 59 | @classmethod 60 | def tearDownClass(cls): 61 | pass 62 | 63 | def test1(self): 64 | arr = [2, 5, 7, 10, 3, 10, -1, -100] 65 | q = Stack() 66 | for x in arr: 67 | q.push(x) 68 | 69 | self.assertEqual(q.getStack(), [-100, -1, 10, 3, 10, 7, 5, 2]) 70 | q.sort() 71 | self.assertEqual(q.getStack(), [-100, -1, 2, 3, 5, 7, 10, 10]) 72 | 73 | 74 | if __name__ == '__main__': 75 | unittest.main() 76 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p03_delete_middle_node/avc278.js: -------------------------------------------------------------------------------- 1 | // Delete Middle Node: Implement an algorithm to delete a node in the middle (i.e. any node but the first and last node, 2 | // not necessarily the exact middle) of a singly linked list, given only access to that node. 3 | // EXAMPLE: 4 | // Input: the node c from the linked list a => b => c => d => e => f 5 | // Result: nothing is returned, but the new linked list looks like a => b => d => e => f 6 | 7 | const assert = require("assert"); 8 | const { 9 | arrayToLinkedList, 10 | compareLinkedLists, 11 | } = require("../../lib/avc278/linkedlist"); 12 | 13 | /** 14 | * Deletes an inputted node somewhere in the middle of the linked list 15 | * @param {LinkedListNode} node input node to be removed 16 | * @return {null} 17 | * 18 | * There's no trick to this problem. We know that a linked list, fundamentally, is just a collection of nodes with 19 | * values that point to another node. We don't need to necessarily delete the selected node. We just need to overwrite 20 | * what that node represents. In other words, we can overwrite the next node onto the current node, then skip from the 21 | * current node to the next next node, effectively skipping over the node after the selected node since it's redundant. 22 | * Runtime: O(1) 23 | * Space: O(1) 24 | * 25 | */ 26 | const deleteMiddleNode = (node) => { 27 | // It is safe to assume from the prompt that the given node exists in the linked list and is not the head nor tail 28 | const next = node.next; 29 | node.val = next.val; 30 | node.next = next.next; 31 | }; 32 | 33 | describe(module.filename, () => { 34 | it("should remove a node in the middle of the linked list", () => { 35 | const arr = [1, 2, 3, 4, 5]; 36 | let ll1 = arrayToLinkedList(arr); 37 | const nodeToBeRemoved = ll1.next.next.next; 38 | const expectedArr = [1, 2, 3, 5]; 39 | let expectedLL1 = arrayToLinkedList(expectedArr); 40 | 41 | deleteMiddleNode(nodeToBeRemoved); 42 | 43 | assert.ok(compareLinkedLists(ll1, expectedLL1)); 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /Python/chapter01/p06_string_compression/miguelHx.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python version 3.7.0 3 | 1.6 - String Compression 4 | Implement a method to perform basic string compression using the counts 5 | of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. 6 | If the "compressed" string would not become smaller than the original string, your method 7 | should return the original string. You can assume the string has only uppercase and lowercase 8 | letters (a-z). 9 | """ 10 | import unittest 11 | 12 | 13 | def str_compression(s: str) -> str: 14 | """ 15 | String compression will attempt to reduce the size of s by condensing 16 | s using the counts of repeated characters. 17 | Runtime: O(n), asymptotic runtime depends on size of s 18 | Space Complexity: O(n), compressed list 19 | :param s: the string we want to compress 20 | :return: the compressed string or s if the compressed string is not smaller 21 | """ 22 | if len(s) == 0: 23 | return s 24 | compressed = [] 25 | prev_char = s[0] 26 | count = 1 27 | for c in s[1:]: 28 | if c == prev_char: 29 | count += 1 30 | continue 31 | compressed.append(f'{prev_char}{count}') 32 | prev_char = c 33 | count = 1 34 | # clean up: last character count 35 | compressed.append(f'{prev_char}{count}') 36 | compressed = ''.join(compressed) 37 | if len(compressed) >= len(s): 38 | return s 39 | return compressed 40 | 41 | 42 | class TestStringCompressionFunction(unittest.TestCase): 43 | def test_string_compression(self): 44 | cases = [ 45 | ('aabcccccaaa', 'a2b1c5a3'), 46 | ('abcde', 'abcde'), 47 | ('aabbccdd', 'aabbccdd'), 48 | ('', ''), 49 | ('c', 'c'), 50 | ('aaAAccCCCCC', 'a2A2c2C5'), 51 | ('aaaaaaaaaa', 'a10') 52 | ] 53 | for s, expected in cases: 54 | self.assertEqual(str_compression(s), expected, msg=(s, expected)) 55 | 56 | 57 | if __name__ == '__main__': 58 | unittest.main() 59 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p02_return_kth_to_last/avc278.js: -------------------------------------------------------------------------------- 1 | // Return Kth to Last: Implement an algorithm to find the kth to last element of a singly linked list. 2 | 3 | const assert = require("assert"); 4 | const { 5 | arrayToLinkedList, 6 | } = require("../../lib/avc278/linkedlist"); 7 | 8 | /** 9 | * Returns the kth to last node in a linked list 10 | * @param {LinkedListNode} list input linked list 11 | * @param {number} k element index from the right which we wish to return 12 | * @return {null} 13 | * 14 | * In this approach, we utilize two pointers, `fast` and `slow` to keep track of how far we've traveled through the 15 | * linked list. We make `fast` travel K steps, before `slow` can start traveling. Then we iterate through both lists 16 | * until `fast` reaches the end. At that point, we know that the `slow` traversal is the kth to last value. 17 | * 18 | * Runtime: O(N) 19 | * Space: O(1) 20 | * 21 | */ 22 | const returnKthToLast = (list, k) => { 23 | let fast = list; 24 | let slow = list; 25 | 26 | while (k > 0) { 27 | if (!fast) return null; 28 | fast = fast.next; 29 | k -= 1; 30 | } 31 | while (fast !== null) { 32 | fast = fast.next; 33 | slow = slow.next; 34 | } 35 | return slow.val; 36 | }; 37 | 38 | describe(module.filename, () => { 39 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 40 | const linkedList = arrayToLinkedList(arr); 41 | for (let i = 1; i < arr.length + 1; i++) { 42 | it(`should return the ${i}th to last element in the linked list`, () => { 43 | const kthElement = returnKthToLast(linkedList, i); 44 | const expectedKthElement = arr[arr.length - i]; 45 | assert.equal(kthElement, expectedKthElement); 46 | }); 47 | } 48 | it("should return null when K is out of bounds", () => { 49 | const smallArr = [1, 2]; 50 | const smallLinkedList = arrayToLinkedList(smallArr); 51 | const bigK = 5; 52 | 53 | const kthElement = returnKthToLast(smallLinkedList, bigK); 54 | const expectedKthElement = null; 55 | assert.equal(kthElement, expectedKthElement); 56 | }); 57 | }); 58 | -------------------------------------------------------------------------------- /Python/chapter01/p07_rotate_matrix/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import random 4 | import unittest 5 | 6 | 7 | def build_matrix(w, h, max=10): 8 | '''function that builds a matrix with random numbers including 0''' 9 | return [[random.randint(0, max) for _ in range(w)] for _ in range(h)] 10 | 11 | def rotate_matrix(matrix): 12 | '''function that rotates a matrix clockwise''' 13 | if (len(matrix) == 0 or len(matrix) is not len(matrix[0])): return False 14 | matrix_length = len(matrix) 15 | for layer in range(matrix_length//2): 16 | first = layer 17 | last = matrix_length - 1 - layer 18 | for i in range(first, last): 19 | offset = i - first 20 | top = matrix[first][i] 21 | 22 | ###################### 23 | # a ----------> d # 24 | # ^ | # 25 | # | | # 26 | # | | # 27 | # | v # 28 | # b <------------ c # 29 | ####################### 30 | 31 | # top left inherits from bottom bottom left 32 | matrix[first][i] = matrix[last - offset][first] 33 | 34 | # bottom left becomes bottom right 35 | matrix[last - offset][first] = matrix[last][last - offset] 36 | 37 | # bottom right becomes top right 38 | matrix[last][last - offset] = matrix[i][last] 39 | 40 | # top right becomes top left 41 | matrix[i][last] = top 42 | 43 | 44 | class Test(unittest.TestCase): 45 | def test_rotate_matrix(self): 46 | matrix = [[8, 0, 5, 6, 8], 47 | [3, 4, 6, 6, 8], 48 | [3, 2, 10, 3, 0], 49 | [4, 3, 9, 0, 2], 50 | [9, 5, 4, 2, 10]] 51 | rotate_matrix(matrix) 52 | self.assertEqual( 53 | matrix, 54 | [[9, 4, 3, 3, 8], 55 | [5, 3, 2, 4, 0], 56 | [4, 9, 10, 6, 5], 57 | [2, 0, 3, 6, 6], 58 | [10, 2, 0, 8, 8]]) 59 | 60 | 61 | if __name__ =='__main__': 62 | unittest.main() 63 | -------------------------------------------------------------------------------- /Python/chapter01/p05_oneaway/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | One Away: There are three types of edits that can be performed on strings: 3 | insert a character, remove a character, or replace a character. Given two 4 | strings, write a function to check if they are one edit (or zero edits) away. 5 | EXAMPLE 6 | pale, ple -> true 7 | pales, pale -> true 8 | pale, bale -> true 9 | pale, bake -> false 10 | Hints:#23, #97, #130 11 | """ 12 | 13 | import unittest 14 | 15 | 16 | def oneAway(str1,str2): 17 | str1 = str1.lower() 18 | str2 = str2.lower() 19 | d = {} 20 | for i in str1: 21 | if i in d: 22 | d[i] =+ 1 23 | else: 24 | d[i] = 1 25 | 26 | for j in str2: 27 | if j in d: 28 | d[j] -= 1 29 | 30 | else: 31 | d[j] = 1 32 | count = 0# count the number of times 33 | 34 | 35 | """ 36 | My thought on how to do this: 37 | So knowing the pale and ple 38 | when we subtract it from the hash table we know there should be a value of 1 39 | for removal of one letter 40 | 41 | The insertation of one letter should only leave the key of one 42 | pales and pale 43 | 44 | The interseting stuff happens when you have a change of letter. 45 | The letter gets counting when you place it in the hash table 46 | pale bale. 47 | We use count to count the number of time count can be one. You'll find 48 | that replaceing a letter is going to make count two v's of one 49 | 50 | replacing two letter will make v's count more onces 51 | 52 | You'll see this when you print k and v 53 | 54 | """ 55 | for k,v in d.items(): 56 | #print(k,v) 57 | if v == 1: 58 | count += 1 59 | 60 | if count <= 2 : # 61 | return True 62 | 63 | else: 64 | return False 65 | 66 | 67 | class Test(unittest.TestCase): 68 | def test_positive(self): 69 | self.assertTrue(oneAway('pale', 'ple')) 70 | self.assertTrue(oneAway('pales', 'pale')) 71 | self.assertTrue(oneAway('pale', 'bale')) 72 | 73 | def test_negative(self): 74 | self.assertFalse(oneAway('pale', 'bake')) 75 | 76 | 77 | if __name__ == '__main__': 78 | unittest.main() 79 | -------------------------------------------------------------------------------- /Python/chapter01/p01_is_unique/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | ''' 3 | Module that implements an algorithm to determine if a string has all unique characters. What if you 4 | cannot use additional data structures? 5 | ''' 6 | 7 | import unittest 8 | 9 | def unique(string): 10 | '''checks if string input is unique 11 | Args: 12 | string (str): string to check 13 | ''' 14 | return len(set(string)) is len(string) 15 | 16 | def unique2(string): 17 | '''checks if string input is unique 18 | args: 19 | string (str): string to check 20 | ''' 21 | letters = {} 22 | for letter in string: 23 | letters[letter] = letters.get(letter, 0) + 1 24 | if letters.get(letter) is not None and letters[letter] > 1: 25 | return False 26 | return True 27 | 28 | def unique3(string): 29 | '''checks if string input is unique, no new data structures 30 | args: 31 | string (str): string to check 32 | ''' 33 | new_string = sorted(string) 34 | for idx in range(len(new_string) - 1): 35 | if new_string[idx] == new_string[idx + 1]: 36 | return False 37 | 38 | return True 39 | 40 | 41 | class Test(unittest.TestCase): 42 | def test1(self): 43 | all_true = ["Nick", "world"] 44 | all_false = ["eee", "hello"] 45 | 46 | for idx in range(len(all_true)): 47 | self.assertTrue(unique(all_true[idx])) 48 | self.assertFalse(unique(all_false[idx])) 49 | 50 | def test2(self): 51 | all_true = ["Nick", "world", "Sue", "John", "abcdefghijklmnopqrstuvwxyz"] 52 | all_false = ["eee", "hello", "nicki", "johnny", "suuu"] 53 | 54 | for idx in range(len(all_true)): 55 | self.assertTrue(unique2(all_true[idx])) 56 | self.assertFalse(unique2(all_false[idx])) 57 | 58 | def test3(self): 59 | all_true = ["Nick", "world", "Sue", "John", "abcdefghijklmnopqrstuvwxyz"] 60 | all_false = ["eee", "hello", "nicki", "johnny", "suuu"] 61 | 62 | for idx in range(len(all_true)): 63 | self.assertTrue(unique3(all_true[idx])) 64 | self.assertFalse(unique3(all_false[idx])) 65 | 66 | if __name__ == '__main__': 67 | unittest.main() 68 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p04_palinperm/avc278.js: -------------------------------------------------------------------------------- 1 | // Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A 2 | // palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. 3 | // The palindrome does not need to be limited to just dictionary words. 4 | 5 | const assert = require("assert"); 6 | 7 | /** 8 | * Checks if the input string can be rearranged into a palindrome 9 | * @param {string} str input string to check against 10 | * @return {boolean} whether the input string can be rearranged into a palindrome 11 | * 12 | * We don't actually have to rearrange the string to solve this problem; we only need to check its contents. 13 | * If the string is of odd length, then each character in the string has to appear an even number of times, except one. 14 | * If the string is of even length, then each character in the string has to appear an even number of times. 15 | * We can create a letter frequency map which takes O(N) time to loop through the string, and O(N) space to build. 16 | * Looping through the object key-value pairs again is an O(N) operation in the worst case where we loop through it all. 17 | * 18 | * Runtime: O(N) 19 | * Space: O(N) 20 | * 21 | */ 22 | const palindromePerm = (str) => { 23 | const letterFreqs = {}; 24 | let length = 0; 25 | for (const letter of str) { 26 | if (letter === " ") continue; 27 | 28 | letterFreqs[letter.toLowerCase()] = 29 | (letterFreqs[letter.toLowerCase()] || 0) + 1; 30 | length += 1; 31 | } 32 | 33 | let slack = length % 2 == 1; 34 | for (const letterFreq of Object.values(letterFreqs)) { 35 | if (letterFreq % 2 === 0) continue; 36 | 37 | if (!slack) return false; 38 | 39 | slack = !slack; 40 | } 41 | 42 | return true; 43 | }; 44 | 45 | describe(module.filename, () => { 46 | it("should return true when the input string can be rearranged in the form of a palindrome.", () => { 47 | assert.ok(palindromePerm("Tact C o a ")); 48 | assert.ok(palindromePerm("RaCeCaR")); 49 | }); 50 | it("should return false when the input string cannot be rearranged in the form of a palindrome.", () => { 51 | assert.ok(!palindromePerm("techqueria")); 52 | }); 53 | }); 54 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p01_is_unique/avc278.js: -------------------------------------------------------------------------------- 1 | // Is Unique: Implement an algorithm to determine if a string has all unique characters. 2 | // What if you cannot used additional data structures? 3 | 4 | const assert = require("assert"); 5 | 6 | /** 7 | * Checks if a string is composed of all unique characters 8 | * @param {string} str input string to check 9 | * @return {bool} Whether the input string contains unique characters 10 | * 11 | * We iterate through the input str of length N. Operations within this loop are O(1), so no additional time added. 12 | * We create an additional data structure, `set` where in the worst case is the same size as the input str, N. 13 | * 14 | * Runtime: O(N) 15 | * Space: O(N) 16 | * 17 | */ 18 | // with additional data structures 19 | const isUnique1 = (str) => { 20 | const strSet = new Set(); 21 | for (const s of str) { 22 | if (strSet.has(s)) return false; 23 | strSet.add(s); 24 | } 25 | return true; 26 | }; 27 | 28 | /** 29 | * Checks if a string is composed of all unique characters 30 | * @param {string} str input string to check 31 | * @return {bool} Whether the input string contains unique characters 32 | * 33 | * We iterate through the input str of length N. Within that, we iterate through the rest of the str of length N-1. 34 | * Within the nested for loop, we only perform an O(1) operation. This leads to O(N^2) for the runtime complexity. 35 | * In this implementation, we do not create any additional data structures, so we use constant space. 36 | * 37 | * Runtime: O(N^2) 38 | * Space: O(1) 39 | */ 40 | // without additional data structures 41 | const isUnique2 = (str) => { 42 | for (let i = 0; i < str.length - 1; i++) { 43 | for (let j = i + 1; j < str.length; j++) { 44 | if (str[i] === str[j]) return false; 45 | } 46 | } 47 | return true; 48 | }; 49 | 50 | describe(module.filename, () => { 51 | it("should return true on an input string with unique characters", () => { 52 | assert.equal(isUnique1("tech"), true); 53 | assert.equal(isUnique2("tech"), true); 54 | }); 55 | it("should return false on an input string with non-unique characters", () => { 56 | assert.equal(isUnique1("techqueria"), false); 57 | assert.equal(isUnique2("techqueria"), false); 58 | }); 59 | }); 60 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p03_urlify/avc278.js: -------------------------------------------------------------------------------- 1 | // URLify: Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient 2 | // space at the end to hold the additional characters, and that you are given the "true" length of the string. 3 | 4 | const assert = require("assert"); 5 | 6 | /** 7 | * URLifies an input string by replacing ' ' with '%20' and returning the modified string 8 | * @param {string} str input string to modify 9 | * @param {int} trueLength the "true" length of the str without trailing spaces 10 | * @return {string} output URLified string 11 | * 12 | * Since we cannot mutate a string in javascript, we need to use an external data structure, like an array. 13 | * Creating a new data structure takes up O(N) space, where N is the length of the str. 14 | * We can loop through the array from the end of the true length to the start, while keeping count of two indexes. 15 | * If we encounter a space within the parameters of the word's true length, we can overwrite the element at that index 16 | * and the previous two indices with "%20", and decrement the index by 3, all of which are O(1) operations. 17 | * Alteranatively, we just overwrite the index of the str length counter with the index of the true length counter, 18 | * and decrement the index by 1, which is also just an O(1) operation. 19 | * When we're done traversing the array, we can simply return the joined array to return a string. 20 | * 21 | * Runtime: O(N) 22 | * Space: O(N) 23 | * 24 | */ 25 | const urlify = (str, trueLength) => { 26 | const strArr = str.split(""); 27 | let idx = str.length - 1; 28 | 29 | for (let i = trueLength - 1; i >= 0; i--) { 30 | if (strArr[i] === " ") { 31 | strArr[idx] = "0"; 32 | strArr[idx - 1] = "2"; 33 | strArr[idx - 2] = "%"; 34 | idx -= 3; 35 | } else { 36 | strArr[idx] = strArr[i]; 37 | idx -= 1; 38 | } 39 | } 40 | return strArr.join(""); 41 | }; 42 | 43 | describe(module.filename, () => { 44 | it("should return the modified string with %20 in place of spaces", () => { 45 | assert.equal(urlify("Mr John Smith ", 13), "Mr%20John%20Smith"); 46 | }); 47 | it("should not modify a string with no spaces", () => { 48 | assert.equal(urlify("techqueria", 10), "techqueria"); 49 | }); 50 | }); 51 | -------------------------------------------------------------------------------- /Python/chapter03/p01_three_in_one/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import logging 4 | import unittest 5 | 6 | 7 | def breakIntoThrees(arr): 8 | if len(arr) <= 2: 9 | logging.debug('Cannot break into 3 pieces') 10 | return 11 | 12 | logging.debug('Breaking array into three pieces %r', arr) 13 | breakpoint = len(arr) // 3 14 | s1, s2, s3 = Stack(), Stack(), Stack() 15 | inputIntoThrees(s1, arr[:breakpoint]) 16 | inputIntoThrees(s2, arr[breakpoint:breakpoint * 2]) 17 | inputIntoThrees(s3, arr[breakpoint * 2:]) 18 | return s1, s2, s3 19 | 20 | 21 | def inputIntoThrees(dastack, arr): 22 | logging.debug('Creating a new stack') 23 | for x in arr: 24 | dastack.push(x) 25 | logging.debug('') 26 | 27 | 28 | class Stack: 29 | def __init__(self): 30 | self.stack = [] 31 | 32 | def pop(self): 33 | if not self.length: 34 | logging.debug('No stack to pop') 35 | return 36 | 37 | item = self.stack.pop() 38 | logging.debug('Item popped', item) 39 | return item 40 | 41 | def length(self): 42 | return len(self.stack) 43 | 44 | def push(self, value): 45 | self.stack.append(value) 46 | logging.debug('Item added to stack', value) 47 | 48 | def peek(self): 49 | if not self.length: 50 | logging.debug('No stack to peek') 51 | return 52 | logging.debug('Peeking into stack', self.stack[-1]) 53 | 54 | def toList(self): 55 | if not self.length: 56 | logging.debug('No stack to peeek') 57 | return [] 58 | 59 | return self.stack[::-1] 60 | 61 | 62 | class Test(unittest.TestCase): 63 | @classmethod 64 | def setUpClass(cls): 65 | pass 66 | 67 | @classmethod 68 | def tearDownClass(cls): 69 | pass 70 | 71 | def test1(self): 72 | arr = [2, 5, 7, 10, 3, -1] 73 | s1, s2, s3 = breakIntoThrees(arr) 74 | self.assertEqual(s1.toList(), [5, 2]) 75 | self.assertEqual(s2.toList(), [10, 7]) 76 | self.assertEqual(s3.toList(), [-1, 3]) 77 | 78 | arr1 = [2, 1, 4, 4, 6, 6, 6] 79 | s3, s4, s5 = breakIntoThrees(arr1) 80 | self.assertEqual(s5.toList(), [6, 6, 6]) 81 | 82 | if __name__ == '__main__': 83 | unittest.main() 84 | -------------------------------------------------------------------------------- /Python/chapter01/p04_palinperm/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | ''' 3 | Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. 4 | A palindrome is a word or phrase that is the same forwards and backwards. A permutation 5 | is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words 6 | ''' 7 | 8 | import unittest 9 | import collections 10 | 11 | def palperm(string): 12 | ''' 13 | Function that checks if it is a permutation of a palindrome 14 | Args: 15 | string (str): a string to check for all palindrome permutations 16 | ''' 17 | table = {} 18 | for letter in string.lower().replace(" ", ""): 19 | table[letter] = table.get(letter, 0) + 1 20 | 21 | count = 0 22 | for key in table: 23 | if table[key] % 2 == 1: 24 | count += 1 25 | if count > 1: 26 | return False 27 | return True 28 | 29 | def palperm2(string): 30 | letter_frequencies = collections.defaultdict(int) 31 | for letter in string.lower().replace(' ', ''): 32 | letter_frequencies[letter] += 1 33 | 34 | odd_frequency_count = 0 35 | for frequency in letter_frequencies.values(): 36 | if frequency % 2 == 0: 37 | continue 38 | odd_frequency_count += 1 39 | if odd_frequency_count > 1: 40 | return False 41 | return True 42 | 43 | class Test(unittest.TestCase): 44 | def test1(self): 45 | input_string ="Tact Coa" 46 | input_string2 ="nick" 47 | input_string3 ="saippuakivikauppias" 48 | input_string4 = "iasppaukivikauppias" 49 | self.assertTrue(palperm(input_string)) 50 | self.assertFalse(palperm(input_string2)) 51 | self.assertTrue(palperm(input_string3)) 52 | self.assertTrue(palperm(input_string4)) 53 | 54 | def test2(self): 55 | input_string ="Tact Coa" 56 | input_string2 ="nick" 57 | input_string3 ="saippuakivikauppias" 58 | input_string4 = "iasppaukivikauppias" 59 | self.assertTrue(palperm2(input_string)) 60 | self.assertFalse(palperm2(input_string2)) 61 | self.assertTrue(palperm2(input_string3)) 62 | self.assertTrue(palperm2(input_string4)) 63 | 64 | if __name__ == '__main__': 65 | unittest.main() 66 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p05_oneaway/rroque98_sol.js: -------------------------------------------------------------------------------- 1 | /* There are 3 types of edits that can be made on a string: 2 | insert character, remove character, replace a character 3 | Given 2 strings write a function that will check if it is 4 | 1 or 0 edits away*/ 5 | 6 | const isOneAway = (str1, str2) => { 7 | const diffInLen = Math.abs(str1.length - str2.length); 8 | if (diffInLen > 1) { 9 | return false; 10 | } 11 | 12 | let errorCount = 0; 13 | if (str1.length === str2.length) { 14 | for (let i = 0; i < str1.length; i++) { 15 | if (str1[i] !== str2[i]) { 16 | errorCount++; 17 | if (errorCount > 1) { 18 | return false; 19 | } 20 | } 21 | } 22 | return true; 23 | } 24 | const longStr = str1.length > str2.length ? str1 : str2; 25 | const shortStr = str1.length <= str2.length ? str1 : str2; 26 | for (let i = 0; i + errorCount < longStr.length; i++) { 27 | if (longStr[i + errorCount] === shortStr[i]) { 28 | continue; 29 | } 30 | errorCount++; 31 | if (errorCount > 1) { 32 | return false; 33 | } 34 | } 35 | return true; 36 | }; 37 | 38 | const assert = require('assert'); 39 | 40 | function runTests(cases, expected) { 41 | for (const [str1, str2] of cases) { 42 | assert.equal(isOneAway(str1, str2), expected); 43 | assert.equal(isOneAway(str2, str1), expected); 44 | } 45 | } 46 | 47 | describe(module.filename, () => { 48 | it('should handle positive cases', () => { 49 | runTests( 50 | [ 51 | ['pale', 'ple'], // deletion 52 | ['pale', 'opale'], // insertion in beginning 53 | ['pale', 'palse'], // insertion in middle 54 | ['pale', 'pales'], // insertion at end 55 | ['pale', 'bale'], // replacement 56 | ['p', 'b'], 57 | ['p', 'p'], 58 | ['p', ''], 59 | ['', ''] 60 | ], 61 | true 62 | ); 63 | }); 64 | 65 | it('should handle negative cases', () => { 66 | runTests( 67 | [ 68 | ['pale', 'ae'], // greater than 1 deletions 69 | ['pale', 'ppalpe'], // greater than 1 insertions 70 | ['pale', 'bake'], // greater than 1 replacements 71 | ['pale', 'balpe'], // 1 insertion, 1 replacement 72 | ['pale', 'plo'], // 1 deletion, 1 replacement 73 | ['pale', 'ales'] // 1 deletion, 1 insertion 74 | ], 75 | false 76 | ); 77 | }); 78 | }); 79 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p06_string_compression/avc278.js: -------------------------------------------------------------------------------- 1 | // String Compression: Implement a method to perform basic string compression using the counts of repeated characters. 2 | // For example, the string aabcccccaaa would become a2b1c5a3. If the "compressed" string would not become smaller than 3 | // the original string, your method should return the origin string. You can assume the string has only uppercase and 4 | // lowercase letters (a-z). 5 | 6 | const assert = require("assert"); 7 | 8 | /** 9 | * Returns the compressed version of the string if applicable 10 | * @param {string} str input string to compress 11 | * @return {string} either the compressed string, or the string 12 | * 13 | * Storing the outputStrArr is at worst size 2N for an input string whose size is N (still O(N) additional space). 14 | * Storing and updating the pointers `idx`, `next`, and `freq` take O(1) additional space and O(1) time. 15 | * As we iterate through the str in the outer while loop, in one extreme, assuming no repeating letters in a sequence, 16 | * idx increments by 1 each iteration, and so it's O(N) runtime. On the other extreme, assuming all repeating letters, 17 | * this would be O(N) on the outer loop, and O(N) in the inner while loop. The trick to knowing this is still O(N) 18 | * and not O(N^2) is the fact that we don't visit any one index pointer more than once in our two while loops. 19 | * Both loops are still just part of the same loop, which results in O(N). 20 | * 21 | * Runtime: O(N) 22 | * Space: O(N) 23 | * 24 | */ 25 | const compress = (str) => { 26 | let outputStrArr = []; 27 | let idx = 0; 28 | 29 | while (idx < str.length) { 30 | const currLetter = str[idx]; 31 | let freq = 1; 32 | 33 | let next = idx + 1; 34 | while (next < str.length && currLetter === str[next]) { 35 | next += 1; 36 | freq += 1; 37 | } 38 | 39 | outputStrArr.push(currLetter); 40 | outputStrArr.push(freq); 41 | idx = next; 42 | } 43 | 44 | return outputStrArr.length < str.length ? outputStrArr.join("") : str; 45 | }; 46 | 47 | describe(module.filename, () => { 48 | it("should compress a string with repeated characters.", () => { 49 | assert.ok(compress("aabcccccaaa", "a2b1c5a3")); 50 | assert.ok(compress("aaaaaaaaaaa", "a11")); 51 | }); 52 | it("should not compress a string whose str length is less than the compressed version.", () => { 53 | assert.equal(compress("tech"), "tech"); 54 | assert.equal(compress("abcdefg"), "abcdefg"); 55 | assert.equal(compress(""), ""); 56 | }); 57 | }); 58 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p08_zero_matrix/rroque98_sol.js: -------------------------------------------------------------------------------- 1 | /*Write an algorithm such that if an element 2 | in an M*N matrix is 0,it's entire row and column 3 | are set to 0*/ 4 | 5 | const zeroMatrix = nestedArr => { 6 | const rowsAndColsWithZeros = checkForZeroIndex(nestedArr); 7 | const rowLength = nestedArr[0].length; 8 | const columnLength = nestedArr.length; 9 | for (const row in rowsAndColsWithZeros['rows']) { 10 | for (let col = 0; col < rowLength; col++) { 11 | nestedArr[row][col] = 0; 12 | } 13 | } 14 | for (const col in rowsAndColsWithZeros['columns']) { 15 | for (let row = 0; row < columnLength; row++) { 16 | nestedArr[row][col] = 0; 17 | } 18 | } 19 | return nestedArr; 20 | }; 21 | 22 | // **** Helper function **** 23 | function checkForZeroIndex(nestArr) { 24 | let zeroIndices = { rows: {}, columns: {} }; 25 | for (let row = 0; row < nestArr.length; row++) { 26 | for (let col = 0; col < nestArr[row].length; col++) { 27 | if (nestArr[row][col] === 0) { 28 | zeroIndices['rows'][row] = true; 29 | zeroIndices['columns'][col] = true; 30 | } 31 | } 32 | } 33 | return zeroIndices; 34 | } 35 | 36 | // **** TESTS ****: 37 | const assert = require('assert'); 38 | 39 | describe(module.filename, () => { 40 | it('should correctly zero out matrices', () => { 41 | let actual = zeroMatrix([[]]); 42 | let expected = [[]]; 43 | assert.deepEqual(actual, expected); 44 | 45 | actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]]); 46 | expected = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]]; 47 | assert.deepEqual(actual, expected); 48 | 49 | actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]]); 50 | expected = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]; 51 | assert.deepEqual(actual, expected); 52 | 53 | actual = zeroMatrix([[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]]); 54 | expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]; 55 | assert.deepEqual(actual, expected); 56 | }); 57 | 58 | it('should correctly find zero indices', () => { 59 | actual = checkForZeroIndex([[1, 2, 0]]); 60 | expected = { rows: { 0: true }, columns: { 2: true } }; 61 | assert.deepEqual(actual, expected); 62 | 63 | actual = checkForZeroIndex([[0]]); 64 | expected = { rows: { 0: true }, columns: { 0: true } }; 65 | assert.deepEqual(actual, expected); 66 | 67 | actual = checkForZeroIndex([[1, 2, 3], [4, 0, 5], [6, 0, 8]]); 68 | expected = { rows: { 1: true, 2: true }, columns: { 1: true } }; 69 | assert.deepEqual(actual, expected); 70 | }); 71 | }); 72 | -------------------------------------------------------------------------------- /Python/chapter03/p06_animal_shelter/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | '''Animal Shelter: An animal shelter, which holds only 3 | dogs and cats, operates on a strictly"first in, first 4 | out" basis. People must adopt either the "oldest" 5 | (based on arrival time) of all animals at the shelter, 6 | or they can select whether they would prefer a dog or 7 | a cat (and will receive the oldest animal of that type). 8 | They cannot select which specific animal they would 9 | like. Create the data structures to maintain this 10 | system and implement operations such as enqueue, 11 | dequeueAny, dequeueDog, and dequeueCat. You may use 12 | the built-in Linked list data structure. 13 | Hints: #22, #56, #63''' 14 | import unittest 15 | 16 | 17 | class ShelterQueue: 18 | def __init__(self): 19 | self.queue = [] 20 | 21 | def enqueue(self, animal): 22 | if len(self.queue) == 0: 23 | self.queue.append(animal) 24 | else: 25 | self.queue.insert(0, animal) 26 | 27 | def printQueue(self): 28 | print('----- Beginning of Queue ------') 29 | for x in self.queue[::-1]: 30 | print(x) 31 | print('------ End of Queue ------') 32 | 33 | def dequeueAnimal(self, animal): 34 | if len(self.queue) == 0: 35 | return 36 | temp = False 37 | for x in self.queue[::-1]: 38 | if animal in x: 39 | temp = x 40 | break 41 | if temp: 42 | pet = temp 43 | self.queue.remove(temp) 44 | return pet 45 | 46 | def dequeueDog(self): 47 | return self.dequeueAnimal('D') 48 | 49 | def dequeueCat(self): 50 | return self.dequeueAnimal('C') 51 | 52 | def dequeueAny(self): 53 | if len(self.queue) == 0: 54 | return 55 | return self.queue.pop() 56 | 57 | 58 | class Test(unittest.TestCase): 59 | def test1(self): 60 | q = ShelterQueue() 61 | animals = ['D1', 'D2', 'C1', 'D3', 'C2', 'C3', 'C4'] 62 | for x in animals: 63 | q.enqueue(x) 64 | 65 | self.assertEqual(q.dequeueDog(), 'D1') 66 | self.assertEqual(q.dequeueCat(), 'C1') 67 | 68 | self.assertEqual(q.dequeueAny(), 'D2') 69 | self.assertEqual(q.dequeueCat(), 'C2') 70 | self.assertEqual(q.dequeueAny(), 'D3') 71 | self.assertEqual(q.dequeueDog(), None) 72 | self.assertEqual(q.dequeueAny(), 'C3') 73 | self.assertEqual(q.dequeueCat(), 'C4') 74 | self.assertEqual(q.dequeueCat(), None) 75 | 76 | 77 | if __name__ == '__main__': 78 | unittest.main() 79 | -------------------------------------------------------------------------------- /Python/chapter01/p02_check_perm/miguelHx.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python version 3.7.0 3 | 1.2 - Check Permutation 4 | Given two strings, write a method to decide if one is a permutation of the other. 5 | """ 6 | import unittest 7 | import collections 8 | from typing import Callable 9 | 10 | 11 | def check_permutation(s1: str, s2: str) -> bool: 12 | """ 13 | Determines if a s1 is a permutation of s2. 14 | s1 is a permutation of s2 if the characters from s1 map to 15 | the same characters in s2 in a one-to-one correspondence AND s1 and s2 are of the same length. 16 | Runtime: O(n + m), where n is the length of s1 and m is the length of s2. 17 | Given: Expect: 18 | "tacos", "costa" True 19 | "tacoos", "costa" False 20 | "swag", "hello" False 21 | "hi", "hello" False 22 | "hola", "loha" True 23 | "p3nding", "dingp3n" True 24 | "", "test" False 25 | "", "" True 26 | "test2", "" False 27 | :param s1: string of size n 28 | :param s2: string of size m 29 | :return: True if s1 is a permutation of s2, False otherwise 30 | """ 31 | # precondition for a permutation, must be same length 32 | if len(s1) != len(s2): 33 | return False 34 | # build histogram of seen characters in s1 and s2 35 | # using histogram because string could have repeated characters 36 | freqs_s1 = collections.Counter(s1) 37 | freqs_s2 = collections.Counter(s2) 38 | # compare frequencies of characters for s1 and s2 39 | for key, val in freqs_s1.items(): 40 | if val != freqs_s2[key]: 41 | # character counts between s1 and s2 don't match 42 | return False 43 | return True 44 | 45 | 46 | class TestCheckPermutationFunction(unittest.TestCase): 47 | def _run_tests(self, f: Callable[[str, str], None]) -> None: 48 | false_cases = [ 49 | ("tacoos", "costa"), ("swag", "hello"), ("hi", "hello"), ("tacooos", "costaaa"), 50 | ("", "test"), ("test2", "") 51 | ] 52 | for case in false_cases: 53 | self.assertFalse(f(case[0], case[1]), msg=case) 54 | 55 | true_cases = [ 56 | ("tacos", "costa"), ("hola", "loha"), ("p3nding", "dingp3n"), ("same", "same"), ("", "") 57 | ] 58 | for case in true_cases: 59 | self.assertTrue(f(case[0], case[1]), msg=case) 60 | with self.assertRaises(TypeError): 61 | f(8) 62 | 63 | def test_check_permutation(self): 64 | self._run_tests(check_permutation) 65 | 66 | 67 | if __name__ == '__main__': 68 | unittest.main() 69 | -------------------------------------------------------------------------------- /Python/chapter02/p04_partition/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import logging 4 | import unittest 5 | 6 | 7 | class Node: 8 | def __init__(self, data, next=None): 9 | self.data = data 10 | self.next = next 11 | 12 | def __str__(self): 13 | return str(self.data) 14 | 15 | 16 | class SinglyLinkedList: 17 | def __init__(self): 18 | self.head = None 19 | 20 | def addNode(self, data): 21 | node = Node(data) 22 | if self.head is None: 23 | self.head = node 24 | else: 25 | temp = self.head 26 | while temp.next: 27 | temp = temp.next 28 | temp.next = node 29 | logging.debug('Node added to LL---> %r', data) 30 | 31 | def toList(self): 32 | if self.head is None: 33 | return [] 34 | 35 | temp = self.head 36 | result = [] 37 | while temp: 38 | result.append(temp.data) 39 | temp = temp.next 40 | return result 41 | 42 | def partition(self, part): 43 | '''However, in a linked list, the situation is much easier. Rather than shifting and swapping elements, we can actually create two different linked lists: one for elements less than x, and one for elements greater than or equal to x.''' 44 | if self.head is None: 45 | logging.debug('No LinkedList to print') 46 | return 47 | 48 | temp = self.head 49 | before = SinglyLinkedList() 50 | after = SinglyLinkedList() 51 | while temp.next: 52 | if temp.data >= part: 53 | after.addNode(temp.data) 54 | else: 55 | before.addNode(temp.data) 56 | temp = temp.next 57 | 58 | # combine the two linked lists 59 | self.head = before.head 60 | temp = before.head 61 | while temp.next: 62 | temp = temp.next 63 | temp.next = after.head 64 | 65 | 66 | class Test(unittest.TestCase): 67 | 68 | @classmethod 69 | def setUpClass(cls): 70 | cls.l = SinglyLinkedList() 71 | cls.l.addNode(3) 72 | cls.l.addNode(20) 73 | cls.l.addNode(21) 74 | cls.l.addNode(5) 75 | cls.l.addNode(8) 76 | cls.l.addNode(5) 77 | cls.l.addNode(10) 78 | cls.l.addNode(2) 79 | cls.l.addNode(1) 80 | 81 | @classmethod 82 | def tearDownClass(cls): 83 | pass 84 | 85 | def test1(self): 86 | self.assertEqual(self.l.toList(), [3, 20, 21, 5, 8, 5, 10, 2, 1]) 87 | self.l.partition(5) 88 | # Note that this code has a bug: it loses the final 1! 89 | self.assertEqual(self.l.toList(), [3, 2, 20, 21, 5, 8, 5, 10]) 90 | 91 | 92 | if __name__ == '__main__': 93 | unittest.main() 94 | -------------------------------------------------------------------------------- /Python/chapter02/p05_sum_lists/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import logging 4 | import unittest 5 | 6 | 7 | class Node: 8 | def __init__(self, data, next=None): 9 | self.data = data 10 | self.next = next 11 | 12 | 13 | class SinglyLinkedList: 14 | def __init__(self): 15 | self.head = None 16 | 17 | def addNode(self, data): 18 | node = Node(data) 19 | if not self.head: 20 | self.head = node 21 | else: 22 | temp = self.head 23 | while temp.next: 24 | temp = temp.next 25 | temp.next = node 26 | logging.debug("Node added --> %r", data) 27 | 28 | def findLength(self, ll): 29 | if not ll.head: 30 | logging.debug('No list to traverse') 31 | return 32 | temp = ll.head 33 | count = 0 34 | while temp: 35 | count += 1 36 | temp = temp.next 37 | return count 38 | 39 | def turnIntoNum(self, ll, direction=True): 40 | '''if direction = true, then sum in place 41 | if direction = false, then reverse the ll to sum''' 42 | 43 | if not ll.head: 44 | logging.debug('No list to traverse') 45 | return 46 | num = 0 47 | length = self.findLength(ll) 48 | if direction: 49 | power = length - 1 50 | n = 0 51 | temp = ll.head 52 | while n < length: 53 | if direction: 54 | num += temp.data * (10 ** power) 55 | power -= 1 56 | else: 57 | num += temp.data * (10 ** n) 58 | temp = temp.next 59 | n += 1 60 | logging.debug("Number to sum: %r", num) 61 | return num 62 | 63 | def addTwoLinkedLists(self, l1, l2, direction=True): 64 | num1 = self.turnIntoNum(l1, direction) 65 | num2 = self.turnIntoNum(l2, direction) 66 | if num1 and num2: 67 | return num1 + num2 68 | else: 69 | logging.debug('No numbers to return') 70 | return 71 | 72 | 73 | class Test(unittest.TestCase): 74 | @classmethod 75 | def setUpClass(cls): 76 | cls.l = SinglyLinkedList() 77 | cls.l.addNode(7) 78 | cls.l.addNode(1) 79 | cls.l.addNode(6) 80 | 81 | cls.k = SinglyLinkedList() 82 | cls.k.addNode(5) 83 | cls.k.addNode(9) 84 | cls.k.addNode(2) 85 | 86 | @classmethod 87 | def tearDownClass(cls): 88 | pass 89 | 90 | def test1(self): 91 | sumReverse = self.l.addTwoLinkedLists(self.l, self.k, False) 92 | self.assertEqual(sumReverse, 912) 93 | sumLL = self.l.addTwoLinkedLists(self.l, self.k) 94 | self.assertEqual(sumLL, 1308) 95 | 96 | 97 | if __name__ == '__main__': 98 | unittest.main() 99 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | 3 | # Logs 4 | logs 5 | *.log 6 | 7 | # Mac specific 8 | DS_Store 9 | 10 | *.gem 11 | *.rbc 12 | /.config 13 | /coverage/ 14 | /InstalledFiles 15 | /pkg/ 16 | /spec/reports/ 17 | /spec/examples.txt 18 | /test/tmp/ 19 | /test/version_tmp/ 20 | /tmp/ 21 | 22 | # Used by dotenv library to load environment variables. 23 | # .env 24 | 25 | ## Specific to RubyMotion: 26 | .dat* 27 | .repl_history 28 | build/ 29 | *.bridgesupport 30 | build-iPhoneOS/ 31 | build-iPhoneSimulator/ 32 | 33 | ## Specific to RubyMotion (use of CocoaPods): 34 | # 35 | # We recommend against adding the Pods directory to your .gitignore. However 36 | # you should judge for yourself, the pros and cons are mentioned at: 37 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 38 | # 39 | # vendor/Pods/ 40 | 41 | ## Documentation cache and generated files: 42 | /.yardoc/ 43 | /_yardoc/ 44 | /doc/ 45 | /rdoc/ 46 | 47 | ## Environment normalization: 48 | /.bundle/ 49 | /vendor/bundle 50 | /lib/bundler/man/ 51 | 52 | # for a library or gem, you might want to ignore these files since the code is 53 | # intended to run in multiple environments; otherwise, check them in: 54 | # Gemfile.lock 55 | # .ruby-version 56 | # .ruby-gemset 57 | 58 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 59 | .rvmrc 60 | 61 | .DS_Store 62 | 63 | # Binaries for programs and plugins 64 | *.exe 65 | *.exe~ 66 | *.dll 67 | *.so 68 | *.dylib 69 | 70 | # Test binary, build with `go test -c` 71 | *.test 72 | 73 | # Output of the go coverage tool, specifically when used with LiteIDE 74 | *.out 75 | 76 | .DS_Store 77 | 78 | 79 | # Created by https://www.gitignore.io/api/python 80 | 81 | ### Python ### 82 | # Byte-compiled / optimized / DLL files 83 | __pycache__/ 84 | *.py[cod] 85 | *$py.class 86 | 87 | # C extensions 88 | *.so 89 | 90 | # Distribution / packaging 91 | .Python 92 | env/ 93 | build/ 94 | develop-eggs/ 95 | dist/ 96 | downloads/ 97 | eggs/ 98 | .eggs/ 99 | lib64/ 100 | parts/ 101 | sdist/ 102 | var/ 103 | *.egg-info/ 104 | .installed.cfg 105 | *.egg 106 | 107 | # PyInstaller 108 | # Usually these files are written by a python script from a template 109 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 110 | *.manifest 111 | *.spec 112 | 113 | # Installer logs 114 | pip-log.txt 115 | pip-delete-this-directory.txt 116 | 117 | # Unit test / coverage reports 118 | htmlcov/ 119 | .tox/ 120 | .coverage 121 | .coverage.* 122 | .cache 123 | nosetests.xml 124 | coverage.xml 125 | *,cover 126 | .hypothesis/ 127 | 128 | # Translations 129 | *.mo 130 | *.pot 131 | 132 | # Django stuff: 133 | *.log 134 | 135 | # Sphinx documentation 136 | docs/_build/ 137 | 138 | # PyBuilder 139 | target/ 140 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p08_zero_matrix/avc278.js: -------------------------------------------------------------------------------- 1 | // Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and columns are set to 0. 2 | 3 | const assert = require("assert"); 4 | 5 | /** 6 | * Overwrite rows and columns who have at least 1 zero in them with zeros. 7 | * @param {Array.} matrix input 2d array to mutate 8 | * @return {null} 9 | * 10 | * We start by initializing rows and cols arrays with false values. We iterate through the matrix, and when we find a 0, 11 | * we flag its row and column by updating the corresponding indices in rows and cols for future use. 12 | * When we take a second pass at the matrix, we update all rows that have been flagged to 0, and all columns that have 13 | * been flagged to 0, which is O(M + N). 14 | * 15 | * Runtime: O(M * N) 16 | * Space: O(M + N) 17 | * 18 | */ 19 | const zeroMatrix = (arr) => { 20 | if (arr.length === 0 || arr[0].length === 0) return; 21 | const rows = new Array(arr.length).fill(false); 22 | const cols = new Array(arr[0].length).fill(false); 23 | 24 | for (let i = 0; i < arr.length; i++) { 25 | for (let j = 0; j < arr[0].length; j++) { 26 | if (arr[i][j] !== 0) continue; 27 | rows[i] = true; 28 | cols[j] = true; 29 | } 30 | } 31 | 32 | for (let i = 0; i < arr.length; i++) { 33 | if (!rows[i]) continue; 34 | for (let j = 0; j < arr[0].length; j++) { 35 | arr[i][j] = 0; 36 | } 37 | } 38 | 39 | for (let j = 0; j < arr[0].length; j++) { 40 | if (!cols[j]) continue; 41 | for (let i = 0; i < arr.length; i++) { 42 | arr[i][j] = 0; 43 | } 44 | } 45 | }; 46 | 47 | describe(module.filename, () => { 48 | it("should not affect a matrix of size 0", () => { 49 | const matrix = []; 50 | 51 | zeroMatrix(matrix); 52 | assert.deepEqual(matrix, []); 53 | }); 54 | it("should not affect a matrix without 0s", () => { 55 | const matrix = [ 56 | [1, 2, 3], 57 | [4, 5, 6], 58 | [7, 8, 9], 59 | ]; 60 | 61 | zeroMatrix(matrix); 62 | assert.deepEqual(matrix, [ 63 | [1, 2, 3], 64 | [4, 5, 6], 65 | [7, 8, 9], 66 | ]); 67 | }); 68 | it("should update all rows and columns with at least one 0 to all 0s", () => { 69 | const matrix = [ 70 | [0, 2, 3, 1], 71 | [4, 5, 6, 1], 72 | [7, 8, 9, 1], 73 | ]; 74 | 75 | zeroMatrix(matrix); 76 | assert.deepEqual(matrix, [ 77 | [0, 0, 0, 0], 78 | [0, 5, 6, 1], 79 | [0, 8, 9, 1], 80 | ]); 81 | }); 82 | it("should update all rows and columns with at least one 0 to all 0s when multiple 0s are present", () => { 83 | const matrix = [ 84 | [0, 2, 3], 85 | [4, 5, 0], 86 | [7, 8, 9], 87 | ]; 88 | 89 | zeroMatrix(matrix); 90 | assert.deepEqual(matrix, [ 91 | [0, 0, 0], 92 | [0, 0, 0], 93 | [0, 8, 0], 94 | ]); 95 | }); 96 | }); 97 | -------------------------------------------------------------------------------- /Python/chapter01/p01_is_unique/miguelHx.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python version 3.7.0 3 | 1.1 4 | Implement an algorithm to determine if a string has all unique characters. 5 | What if you cannot use additional data structures? 6 | What if the input string is empty? 7 | I assume that the user will input a non-empty string 8 | If the string is empty, then raise a ValueError exception 9 | If the input is NOT a string, then raise a TypeError exception 10 | """ 11 | import unittest 12 | from typing import Callable 13 | 14 | 15 | def is_unique(input_str: str) -> bool: 16 | """ 17 | Determines if input_str has all unique characters. 18 | For all characters in the input_str with n >= 1, where n is the number of characters in input_str, 19 | there must be no duplicate characters. 20 | In the worst case, the run time of this function will be O(n) 21 | Space complexity will be O(n) 22 | Given: Expect: 23 | tacos True 24 | swag True 25 | bobby False 26 | california False 27 | orbit True 28 | e True 29 | "" True 30 | :param input_str: the string we want to check characters of 31 | :return: returns True if input_str has all unique characters, False otherwise 32 | """ 33 | chars_seen = set() 34 | for c in input_str: 35 | if c in chars_seen: 36 | return False 37 | chars_seen.add(c) 38 | return True 39 | 40 | 41 | def is_unique_no_additional_data_structures(input_str: str) -> bool: 42 | """ 43 | Variant of is_unique. Uses no additional data structures (besides the variables from the iterator) 44 | However, the drawback is that the runtime is O((n*n-1)/2) = O(n^2), where n is the number of characters 45 | in the input_str. 46 | Given: Expect: 47 | tacos True 48 | swag True 49 | bobby False 50 | california False 51 | orbit True 52 | e True 53 | "" True 54 | :param input_str: the string we want to check characters of 55 | :return: returns True if input_str has all unique characters, False otherwise 56 | """ 57 | for i, c in enumerate(input_str): 58 | if c in input_str[i+1:]: 59 | return False 60 | return True 61 | 62 | 63 | class TestIsUniqueFunction(unittest.TestCase): 64 | def _run_tests(self, f: Callable[[str], None]) -> None: 65 | for case in ["techqueria", "bobby", "california"]: 66 | self.assertFalse(f(case), msg=case) 67 | for case in ["tacos", "swag", "orbit", "e", ""]: 68 | self.assertTrue(f(case), msg=case) 69 | with self.assertRaises(TypeError): 70 | f(8) 71 | 72 | def test_is_unique(self): 73 | self._run_tests(is_unique) 74 | 75 | def test_is_unique_no_additional_data_structures(self): 76 | self._run_tests(is_unique_no_additional_data_structures) 77 | 78 | 79 | if __name__ == '__main__': 80 | unittest.main() 81 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p06_palindrome/avc278.js: -------------------------------------------------------------------------------- 1 | // Palindrome: Implement a function to check if a linked list is a palindrome. 2 | 3 | const assert = require("assert"); 4 | const { arrayToLinkedList } = require("../../lib/avc278/linkedlist"); 5 | 6 | /** 7 | * Checks whether the input linked list is a palindrome 8 | * @param {LinkedListNode} list input linked list to check against 9 | * @return {boolean} whether the input linked list is a palindrome 10 | * 11 | * For this problem, we start by finding the midpoint idx of the linked list. This traversal takes O(N) runtime and O(1) 12 | * space as we only store a counter, and a pointer to the location within the linked list. We then perform another 13 | * traversal to point to the midpoint, and reverse the linked list from that point onwards. From this point onwards, we 14 | * only need to compare the two linked lists from the midpoint to the end, and from the start to the midpoint. 15 | * All in all, since we only perform traversals without any inner traversals, the runtime at any traversal is O(N), 16 | * where N is the length of the linked list, and O(1) space as we store pointers. 17 | * Runtime: O(N) 18 | * Space: O(1) 19 | * 20 | */ 21 | const reverse = (node) => { 22 | let prev; 23 | while (node !== null) { 24 | const next = node.next; 25 | node.next = prev; 26 | prev = node; 27 | node = next; 28 | } 29 | return prev; 30 | }; 31 | 32 | const isPalindrome = (list) => { 33 | let fast = list; 34 | let slow = list; 35 | let counter = 0; 36 | while (fast !== null) { 37 | fast = fast.next; 38 | counter += 1; 39 | } 40 | 41 | for (let i = 0; i < counter / 2; i++) { 42 | slow = slow.next; 43 | } 44 | 45 | slow = reverse(slow); 46 | while (!!slow && !!slow.next) { 47 | if (slow.val !== list.val) return false; 48 | slow = slow.next; 49 | list = list.next; 50 | } 51 | 52 | return true; 53 | }; 54 | 55 | describe(module.filename, () => { 56 | it("should return true when the linked list of even length is a palindrome", () => { 57 | const arr = [1, 2, 3, 4, 4, 3, 2, 1]; 58 | const ll = arrayToLinkedList(arr); 59 | 60 | assert.ok(isPalindrome(ll)); 61 | }); 62 | it("should return true when the linked list of odd length is a palindrome", () => { 63 | const arr = [1, 2, 3, 4, 3, 2, 1]; 64 | const ll = arrayToLinkedList(arr); 65 | 66 | assert.ok(isPalindrome(ll)); 67 | }); 68 | it("should return false when the linked list is not a palindrome", () => { 69 | const arr = [1, 2, 3, 4, 3, 2, 1, 0]; 70 | const ll = arrayToLinkedList(arr); 71 | 72 | assert.ok(!isPalindrome(ll)); 73 | }); 74 | it("should return true for a linked list with a single node", () => { 75 | const arr = [2]; 76 | const ll = arrayToLinkedList(arr); 77 | 78 | assert.ok(isPalindrome(ll)); 79 | }); 80 | it("should return true for an empty linked list", () => { 81 | const arr = []; 82 | const ll = arrayToLinkedList(arr); 83 | 84 | assert.ok(isPalindrome(ll)); 85 | }); 86 | }); 87 | -------------------------------------------------------------------------------- /Python/chapter01/p09_string_rotation/miguelHx.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python version 3.7.0 3 | 1.9 - String Rotation 4 | Assume you have a method isSubstring which 5 | checks if one word is a substring 6 | of another. Given two strings, s1 and s2, 7 | write code to check if s2 is a 8 | rotation of s1 using only one call to isSubstring 9 | (e.g. 'waterbottle' is a rotation of 'erbottlewat') 10 | """ 11 | import unittest 12 | 13 | 14 | def is_substring(sub: str, s: str) -> bool: 15 | """ 16 | is_substring checks if 'sub' is a substring of 's' 17 | :param sub: the substring 18 | :param s: the alleged full string 19 | :return: true if sub is a substring of s, false otherwise 20 | """ 21 | return sub in s 22 | 23 | 24 | def string_rotation(s1: str, s2: str) -> bool: 25 | """ 26 | Given two strings, string_rotation will check if s2 is a rotation of s1 27 | using only one call to isSubstring. 28 | Assume s2 is a rotation of s1. s1 is a rearrangement of s2. 29 | Choosing an index where we rotate to be any index, 30 | let s1 and s2 consist of two parts, a and b. 31 | The original string will be s2 = ab. 32 | The rotated string will be s1 = ba. 33 | Since we are using substring, we want s2 = ab to appear in 34 | a larger string ..ab.. 35 | This is guaranteed to happen if we have s1 + s1 = baba 36 | because ab shows up in there. 37 | Runtime: worst case: O(n^2) - substring found at end of full string 38 | Space Complexity: O(n) 39 | :param s1: the rotated string 40 | :param s2: the original string 41 | :return: true if s2 is a rotation of s1, false otherwise 42 | """ 43 | # not rotation if lengths don't match 44 | if len(s1) != len(s2): 45 | return False 46 | return is_substring(s2, s1 + s1) 47 | 48 | 49 | class TestStringRotation(unittest.TestCase): 50 | 51 | def setUp(self): 52 | self.str_rotation_cases = [ 53 | ('erbottlewat', 'waterbottle', True), 54 | ('erbottlewet', 'weterbottle', True), 55 | ('same', 'same', True), 56 | ('oneoff', 'oneof', False), 57 | ('erbottleabc', 'waterbottle', False), 58 | ('ion?rotat', 'rotation?', True), 59 | ('', '', True), 60 | ('a', 'a', True), 61 | ('a', 'b', False), 62 | ('erbottlewat', 'eeeeeeeeee', False) 63 | ] 64 | 65 | self.is_substring_cases = [ 66 | ('swag', 'swagger', True), 67 | ('swagger', 'swag', False), 68 | ('test', 'testing', True), 69 | ('', 'sdfkjslf', True), 70 | ('abcd', 'defg', False) 71 | ] 72 | 73 | def test_is_substring(self): 74 | for sub, s, expected in self.is_substring_cases: 75 | self.assertEqual(is_substring(sub, s), expected, msg=(sub, s, expected)) 76 | 77 | def test_string_rotation(self): 78 | for s1, s2, expected in self.str_rotation_cases: 79 | self.assertEqual(string_rotation(s1, s2), expected, msg=(s1, s2, expected)) 80 | 81 | 82 | if __name__ == '__main__': 83 | unittest.main() 84 | -------------------------------------------------------------------------------- /Python/chapter01/p05_oneaway/miguelHx.py: -------------------------------------------------------------------------------- 1 | """ 2 | Python version 3.7.0 3 | 1.5 - One Away 4 | There are three types of edits that can be performed on strings: insert a character, 5 | remove a character, or replace a character. Given two strings, write a function to check 6 | if they are one edit (or zero edits) away. 7 | """ 8 | import unittest 9 | from typing import Callable 10 | 11 | 12 | def one_away(s1: str, s2: str) -> bool: 13 | """ 14 | Checks if two strings are one edit or zero edits away. 15 | There are 3 types of edits: 16 | 1. insert a character 17 | 2. remove a character 18 | 3. replace a character 19 | Zero edits away means the two strings are exactly the same. 20 | We will do different checks depending on the size different of the strings. 21 | Runtime: O(n), where n is length of s1 22 | Space Complexity: O(n + n +/- 1) = O(n), where n is length of s1 23 | :param s1: one string we want to compare 24 | :param s2: other string to compare 25 | :return: true if one or zero edits away, false otherwise 26 | """ 27 | # if size differs by more than 1, guaranteed more than one edit distance. 28 | if abs(len(s1) - len(s2)) > 1: 29 | return False 30 | # if size is the same, scan for edits 31 | if len(s1) == len(s2): 32 | edits = 0 33 | for i, c in enumerate(s1): 34 | if c == s2[i]: 35 | continue 36 | edits += 1 37 | if edits > 1: 38 | return False 39 | return True 40 | # otherwise, we have an insertion/deletion 41 | # compare both strings for insertion/deletion 42 | # want to loop through the shorter string to avoid going out of bounds 43 | s_short = s1 if len(s1) < len(s2) else s2 44 | s_long = s1 if len(s1) > len(s2) else s2 45 | 46 | added = 0 47 | for i, c in enumerate(s_short): 48 | if c == s_long[i + added]: 49 | continue 50 | # chars didn't match, will check next char in next iteration 51 | added += 1 52 | if added > 1: 53 | # added more than once, and we did not match characters. 54 | # guaranteed at least 2 edit distance 55 | return False 56 | return True 57 | 58 | 59 | class TestOneAwayFunction(unittest.TestCase): 60 | def _run_tests(self, f: Callable[[str, str], bool]) -> None: 61 | cases = [ 62 | ("pale", "ple", True), 63 | ("pales", "pale", True), 64 | ("pale", "bale", True), 65 | ("pale", "bake", False), 66 | ("", "swag", False), 67 | ("paale", "pale", True), 68 | ("a", "", True), 69 | ("", "b", True), 70 | ("a", "b", True), 71 | ("", "", True), 72 | ("pale", "elap", False), 73 | ("pale", "elaps", False), 74 | ("pale", "palse", True) 75 | ] 76 | for s1, s2, expected in cases: 77 | self.assertEqual(f(s1, s2), expected, msg=(s1, s2)) 78 | 79 | def test_one_away(self): 80 | self._run_tests(one_away) 81 | 82 | 83 | if __name__ == '__main__': 84 | unittest.main() 85 | -------------------------------------------------------------------------------- /Python/chapter02/p06_palindrome/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Palindrome: Implement a function to check if a linked list is a 3 | palindrome. Hints:#5, #13, #29, #61, #101 4 | """ 5 | 6 | import unittest 7 | 8 | 9 | class Node: 10 | #Singly link list 11 | def __init__(self,data = None): 12 | self.data = data 13 | self.next = None 14 | 15 | class linklist: 16 | #linkList class 17 | def __init__(self): 18 | self.head = None 19 | self.size = 0 20 | 21 | def push(self,data): 22 | node = Node(data)# create a new node 23 | 24 | if self.head == None: #check to see if the head node is empty 25 | self.head = node # If its empty add it to the new node 26 | self.size = 1 27 | return 28 | #if the head of the linklist is filled 29 | 30 | current = self.head 31 | while current.next is not None:#Check the current postion is empty 32 | #Move to the next line if nothing is there 33 | current = current.next 34 | 35 | 36 | current.next = node #point self.head to a new node 37 | self.size+=1 38 | 39 | def lenght(self): 40 | #note the count doesn't start at zero 41 | cur = self.head 42 | counter = 0 43 | while cur is not None: 44 | counter+=1 45 | cur = cur.next 46 | print('Linklist len: '+str(counter)) 47 | return counter 48 | 49 | def printList(self): 50 | curr = self.head 51 | elem = [] 52 | 53 | while(curr != None): 54 | elem.append(curr.data) 55 | curr = curr.next 56 | print(elem) 57 | #1->2->3 58 | def remove_node(self,data): 59 | #1->2->3 60 | curr = self.head 61 | if curr is not None and curr.data == data: 62 | self.head = curr.next 63 | curr = None 64 | #look for the node that has the data we are looking for 65 | while curr is not None: 66 | if curr.data == data: 67 | break 68 | prev = curr 69 | curr = curr.next 70 | 71 | #if data isn't found just reutrn 72 | if(curr == None): 73 | return 74 | 75 | #allow us to unlink the nodes 76 | prev.next = curr.next 77 | curr = None 78 | 79 | def palindromeCheck(self): 80 | curr = self.head 81 | str1 = "" 82 | str2 = "" 83 | while curr is not None: 84 | str1 += curr.data 85 | curr = curr.next 86 | 87 | for i in range(len(str1)): 88 | str2+=str1[len(str1) - 1 - i] 89 | 90 | 91 | if str2 == str1: 92 | return True 93 | else: 94 | return False 95 | 96 | 97 | class Test(unittest.TestCase): 98 | def test_palindrome(self): 99 | llist = linklist() 100 | llist.push('a') 101 | llist.push('n') 102 | llist.push('n') 103 | llist.push('a') 104 | self.assertTrue(llist.palindromeCheck()) 105 | 106 | 107 | if __name__ == '__main__': 108 | unittest.main() 109 | -------------------------------------------------------------------------------- /Python/chapter02/p08_loop_detection/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import logging 4 | import unittest 5 | 6 | 7 | class Node: 8 | def __init__(self, data, next=None): 9 | self.data = data 10 | self.next = next 11 | 12 | 13 | class SinglyLinkedList: 14 | def __init__(self): 15 | self.head = None 16 | 17 | def addNode(self, data, next=None): 18 | node = Node(data, next) 19 | if not self.head: 20 | self.head = node 21 | else: 22 | temp = self.head 23 | while temp.next: 24 | temp = temp.next 25 | temp.next = node 26 | logging.debug('Node added to LL-->%r', data) 27 | 28 | def findNode(self, value): 29 | if not self.head: 30 | logging.debug('No list to iterate') 31 | return 32 | 33 | temp = self.head 34 | while temp: 35 | if temp.data == value: 36 | return temp 37 | temp = temp.next 38 | return None 39 | 40 | def toList(self): 41 | temp = self.head 42 | result = [] 43 | while temp: 44 | result.append(temp.data) 45 | temp = temp.next 46 | return result 47 | 48 | def loopDetection(self): 49 | if not self.head: 50 | logging.debug('No list to iterate for loop detection') 51 | return 52 | 53 | slow = self.head 54 | fast = self.head 55 | 56 | while fast and fast.next: 57 | slow = slow.next 58 | fast = fast.next.next 59 | if slow == fast: 60 | break 61 | 62 | if not fast or not fast.next: 63 | return None 64 | 65 | slow = self.head 66 | while slow is not fast: 67 | slow = slow.next 68 | fast = fast.next 69 | 70 | return fast 71 | 72 | 73 | class Test(unittest.TestCase): 74 | @classmethod 75 | def setUpClass(cls): 76 | cls.l = SinglyLinkedList() 77 | cls.l.addNode(3) 78 | cls.l.addNode(1) 79 | cls.l.addNode(5) 80 | cls.l.addNode(9) 81 | cls.l.addNode(7) 82 | cls.l.addNode(2) 83 | cls.l.addNode(10) 84 | 85 | cls.k = SinglyLinkedList() 86 | cls.k.addNode(1) 87 | cls.k.addNode(10) 88 | cls.k.addNode(15) 89 | cls.k.addNode(16) 90 | cls.k.addNode(1) 91 | 92 | @classmethod 93 | def tearDownClass(cls): 94 | pass 95 | 96 | def printStatement(self, node): 97 | if node: 98 | print("Start of Loop", hex(id(node)), '-->', node.data) 99 | else: 100 | print('No loop detected') 101 | 102 | def test1(self): 103 | node = self.l.findNode(10) 104 | node.next = self.l.findNode(5) 105 | # if uncomment below, will print in infinite 106 | # self.l.printList() 107 | self.assertEqual(self.l.loopDetection().data, 5) 108 | 109 | def test2(self): 110 | self.assertIsNone(self.k.loopDetection()) 111 | 112 | 113 | if __name__ == '__main__': 114 | unittest.main() 115 | -------------------------------------------------------------------------------- /JavaScript/chapter02/p01_remove_dups/avc278.js: -------------------------------------------------------------------------------- 1 | // Remove Dups: Write code to remove duplicates from an unsorted linked list. 2 | // Follow Up: How would you solve this problem if a temporary buffer is not allowed? 3 | 4 | const assert = require("assert"); 5 | const { 6 | arrayToLinkedList, 7 | compareLinkedLists, 8 | } = require("../../lib/avc278/linkedlist"); 9 | 10 | /** 11 | * Removes duplicates from a linked list 12 | * @param {LinkedListNode} list input linked list to mutate 13 | * @return {null} 14 | * 15 | * Keep a temporary set for storing values we pass through, of maximum size N where N is the number of nodes in the 16 | * input linked list in the worst case. This requires an additional O(N) space. We store two pointers as we traverse 17 | * through the linked list, skipping nodes with values already in our set. 18 | * Runtime: O(N) 19 | * Space: O(N) 20 | * 21 | */ 22 | const removeDups1 = (list) => { 23 | if (!list) return; 24 | const seen = new Set(); 25 | let currNode = list; 26 | seen.add(currNode.val); 27 | for ( 28 | let nextNode = currNode.next; 29 | nextNode.next !== null; 30 | nextNode = nextNode.next 31 | ) { 32 | if (seen.has(nextNode.val)) continue; 33 | 34 | seen.add(nextNode.val); 35 | currNode.next = nextNode; 36 | currNode = currNode.next; 37 | } 38 | 39 | currNode.next = null; 40 | }; 41 | 42 | /** 43 | * Removes duplicates from a linked list 44 | * @param {LinkedListNode} list input linked list to mutate 45 | * @return {null} 46 | * 47 | * Instead of keeping a temporary set, we need another way to remove all future dupes of the current node. 48 | * One way of doing this, is to skip all future nodes containing the same val as the current one. 49 | * This nest loop through the linked list increases our run time to O(N^2), but since we don't use any additional space, 50 | * we reduced the space complexity to O(1). 51 | * 52 | * Runtime: O(N^2) 53 | * Space: O(1) 54 | * 55 | */ 56 | const removeDups2 = (list) => { 57 | if (!list) return; 58 | let currNode = list; 59 | while (currNode !== null) { 60 | let nextNode = currNode; 61 | while (nextNode.next !== null) { 62 | if (currNode.val === nextNode.next.val) { 63 | nextNode.next = nextNode.next.next; 64 | } else { 65 | nextNode = nextNode.next; 66 | } 67 | } 68 | currNode = currNode.next; 69 | } 70 | }; 71 | 72 | const removeDups = [removeDups1, removeDups2]; 73 | removeDups.forEach((removeDup) => { 74 | describe(removeDup.name, () => { 75 | it("should return the linked list without duplicates", () => { 76 | const arr = [2, 2, 3, 4, 2, 4]; 77 | let ll1 = arrayToLinkedList(arr); 78 | const expectedArr = [2, 3, 4]; 79 | let expectedLL1 = arrayToLinkedList(expectedArr); 80 | 81 | removeDup(ll1); 82 | assert.ok(compareLinkedLists(ll1, expectedLL1)); 83 | }); 84 | it("should return an empty linked list", () => { 85 | const arr = []; 86 | let ll1 = arrayToLinkedList(arr); 87 | const expectedArr = []; 88 | let expectedLL1 = arrayToLinkedList(expectedArr); 89 | 90 | removeDup(ll1); 91 | assert.ok(compareLinkedLists(ll1, expectedLL1)); 92 | }); 93 | }); 94 | }); 95 | -------------------------------------------------------------------------------- /Python/chapter02/p08_loop_detection/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Loop Detection: Given a circular linked list, implement an algorithm that returns the node at the 3 | beginning of the loop. 4 | DEFINITION 5 | Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list. 6 | EXAMPLE 7 | Input: A -> B -> C -> D -> E -> C [thesameCasearlier] 8 | Output: C 9 | Hints: #50, #69, #83, #90 10 | """ 11 | 12 | import logging 13 | import unittest 14 | 15 | class Node: 16 | #Singly link list 17 | def __init__(self,data = None): 18 | self.data = data 19 | self.next = None 20 | 21 | class linklist: 22 | #linkList class 23 | def __init__(self): 24 | self.head = None 25 | self.size = 0 26 | 27 | def getSize(self): 28 | return self.size 29 | 30 | def push(self,data): 31 | node = Node(data)# create a new node 32 | 33 | if self.head == None: #check to see if the head node is empty 34 | self.head = node # If its empty add it to the new node 35 | self.size = 1 36 | return 37 | #if the head of the linklist is filled 38 | 39 | current = self.head 40 | while current.next is not None:#Check the current postion is empty 41 | #Move to the next line if nothing is there 42 | current = current.next 43 | 44 | 45 | current.next = node #point self.head to a new node 46 | self.size+=1 47 | 48 | def lenght(self): 49 | #note the count doesn't start at zero 50 | cur = self.head 51 | counter = 0 52 | while cur is not None: 53 | counter+=1 54 | cur = cur.next 55 | self.logging('Linklist len: %d', counter) 56 | return counter 57 | 58 | #1->2->3 59 | def remove_node(self,data): 60 | #1->2->3 61 | curr = self.head 62 | if curr is not None and curr.data == data: 63 | self.head = curr.next 64 | curr = None 65 | #look for the node that has the data we are looking for 66 | while curr is not None: 67 | if curr.data == data: 68 | break 69 | prev = curr 70 | curr = curr.next 71 | 72 | #if data isn't found just reutrn 73 | if(curr == None): 74 | return 75 | 76 | #allow us to unlink the nodes 77 | prev.next = curr.next 78 | curr = None 79 | def loopDetection(self): 80 | p1 = p2 = self.head 81 | 82 | while p1 and p2: 83 | #runner 84 | p1 = p1.next 85 | p2 = p2.next.next 86 | if p1 == p2: 87 | return True 88 | else: 89 | return False 90 | 91 | 92 | class Test(unittest.TestCase): 93 | def test_loop_detection(self): 94 | llist = linklist() 95 | llist.push(1) 96 | llist.push(2) 97 | llist.push(3) 98 | llist.head.next.next = llist.head.next # make my loop 99 | self.assertTrue(llist.loopDetection()) 100 | 101 | 102 | if __name__ == '__main__': 103 | unittest.main() 104 | -------------------------------------------------------------------------------- /Python/chapter03/p04_queue_via_stacks/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | '''Queue via Stacks: Implement a MyQueue class 3 | which implements a queue using two stacks.''' 4 | 5 | import logging 6 | import unittest 7 | 8 | 9 | class Stack: 10 | def __init__(self): 11 | self.stack = [] 12 | self.min = None 13 | 14 | def getMin(self): 15 | return self.min 16 | 17 | def pop(self): 18 | if not self.length: 19 | logging.debug('No stack to pop') 20 | return 21 | 22 | item = self.stack.pop() 23 | 24 | if item == self.min: 25 | if self.length() > 0: 26 | self.findMin() 27 | else: 28 | self.min = None 29 | return item 30 | 31 | def findMin(self): 32 | self.min = self.stack[0] 33 | for x in self.stack: 34 | if x < self.min: 35 | self.min = x 36 | 37 | def length(self): 38 | return len(self.stack) 39 | 40 | def push(self, value): 41 | if not self.min: 42 | self.min = value 43 | elif value < self.min: 44 | self.min = value 45 | 46 | self.stack.append(value) 47 | 48 | def peek(self): 49 | if not self.length: 50 | logging.debug('No stack to peek') 51 | return 52 | return self.stack[-1] 53 | 54 | def printStack(self): 55 | if not self.length: 56 | logging.debug('No stack to peeek') 57 | return 58 | 59 | logging.debug("Top of stack\n _ ") 60 | for x in self.stack[::-1]: 61 | logging.debug('|%r|', x) 62 | logging.debug('|_|') 63 | 64 | 65 | class MyQueue: 66 | def __init__(self): 67 | self.newStack = Stack() 68 | self.oldStack = Stack() 69 | 70 | def push(self, value): 71 | self.newStack.push(value) 72 | 73 | def pop(self): 74 | self.rotate() 75 | if self.oldStack.length() == 0: 76 | logging.debug('Nothing to pop in this queue') 77 | return 78 | return self.oldStack.pop() 79 | 80 | def peek(self): 81 | self.rotate() 82 | if self.oldStack.length() == 0: 83 | logging.debug('Nothing to peek in this queue') 84 | return 85 | return self.oldStack.peek() 86 | 87 | def rotate(self): 88 | if (self.oldStack.length() == 0): 89 | while not self.newStack.length() == 0: 90 | self.oldStack.push(self.newStack.pop()) 91 | 92 | def printQueue(self): 93 | self.rotate() 94 | for x in self.oldStack.stack[::-1]: 95 | print('Item in Queue -->', x) 96 | 97 | for x in self.newStack.stack: 98 | print('Item in Queue -->', x) 99 | 100 | 101 | class Test(unittest.TestCase): 102 | @classmethod 103 | def setUpClass(cls): 104 | pass 105 | 106 | @classmethod 107 | def tearDownClass(cls): 108 | pass 109 | 110 | def test1(self): 111 | arr = [2, 5, 7, 10, 3, 10, -1, -100] 112 | q = MyQueue() 113 | for x in arr: 114 | q.push(x) 115 | 116 | self.assertEqual(q.pop(), 2) 117 | self.assertEqual(q.peek(), 5) 118 | 119 | 120 | if __name__ == '__main__': 121 | unittest.main() 122 | -------------------------------------------------------------------------------- /Python/chapter02/p02_return_kth_to_last/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """ 2 | Return Kth to Last: Implement an algorithm to find the kth to last element of 3 | a singly linked list. 4 | Hints:#8, #25, #41, #67, #126 5 | """ 6 | 7 | import logging 8 | import unittest 9 | 10 | 11 | class Node: 12 | #Singly link list 13 | def __init__(self,data): 14 | self.data = data 15 | self.next = None 16 | 17 | class linklist: 18 | #linkList class 19 | def __init__(self): 20 | self.head = None 21 | self.size = 0 22 | 23 | def push(self,data): 24 | node = Node(data)# create a new node 25 | 26 | if self.head == None: #check to see if the head node is empty 27 | self.head = node # If its empty add it to the new node 28 | self.size = 1 29 | return 30 | #if the head of the linklist is filled 31 | 32 | current = self.head 33 | while current.next is not None:#Check the current postion is empty 34 | #Move to the next line if nothing is there 35 | current = current.next 36 | 37 | 38 | current.next = node #point self.head to a new node 39 | self.size+=1 40 | 41 | 42 | 43 | 44 | def length(self): 45 | #note the count doesn't start at zero 46 | cur = self.head 47 | counter = 0 48 | while cur is not None: 49 | counter+=1 50 | cur = cur.next 51 | logging.debug('Linklist len: %d', counter) 52 | return counter 53 | 54 | def toList(self): 55 | n = self.head 56 | 57 | result = [] 58 | while(n): 59 | result.append(n.data) 60 | n = n.next 61 | return result 62 | 63 | #1->2->3 64 | def remove_node(self,data): 65 | #1->2->3 66 | curr = self.head 67 | if curr is not None and curr.data == data: 68 | self.head = curr.next 69 | curr = None 70 | #look for the node that has the data we are looking for 71 | while curr is not None: 72 | if curr.data == data: 73 | break 74 | prev = curr 75 | curr = curr.next 76 | 77 | #if data isn't found just reutrn 78 | if(curr == None): 79 | return 80 | 81 | #allow us to unlink the nodes 82 | prev.next = curr.next 83 | curr = None 84 | 85 | def Kth_to_Last(self,num): 86 | counter = 1 # We need to keep track where we are 87 | curr = self.head 88 | goal = self.size - num# kth to last element 89 | while curr is not None: 90 | if goal == counter: 91 | return curr.data 92 | counter+=1 93 | curr = curr.next 94 | 95 | 96 | class Test(unittest.TestCase): 97 | def test_kth_to_the_last(self): 98 | llist = linklist() 99 | llist.push(1) 100 | llist.push(2) 101 | llist.push(3) 102 | llist.push(4) 103 | llist.push(5) 104 | self.assertEqual(llist.toList(), [1, 2, 3, 4, 5]) 105 | 106 | self.assertEqual(llist.length(), 5) 107 | self.assertEqual(llist.Kth_to_Last(1), 4) 108 | self.assertEqual(llist.Kth_to_Last(2), 3) 109 | self.assertEqual(llist.Kth_to_Last(3), 2) 110 | 111 | 112 | if __name__ =='__main__': 113 | unittest.main() 114 | 115 | 116 | -------------------------------------------------------------------------------- /Python/chapter02/p02_return_kth_to_last/nickolasteixeira.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | ''' 3 | Return Kth to Last: Implement an algorithm to 4 | find the kth to last element of a singly linked list. 5 | ''' 6 | 7 | import logging 8 | import unittest 9 | 10 | 11 | class Node: 12 | def __init__(self, data, next=None): 13 | self.data = data 14 | self.next = next 15 | 16 | def __str__(self): 17 | return str(self.data) 18 | 19 | 20 | class SinglyLinkedList: 21 | 22 | def __init__(self): 23 | self.head = None 24 | self.size = 0 25 | 26 | def insertHead(self, data): 27 | logging.debug('Inserting head!-----> %r', data) 28 | n = Node(data) 29 | temp = self.head 30 | self.head = n 31 | self.head.next = temp 32 | 33 | def addNode(self, data): 34 | if not self.head: 35 | n = Node(data) 36 | logging.debug('New Head added ----> %r', data) 37 | self.head = n 38 | else: 39 | n = self.head 40 | while (n.next): 41 | n = n.next 42 | 43 | logging.debug('New Node added ----> %r', data) 44 | new_node = Node(data) 45 | n.next = new_node 46 | 47 | def kthToLastElement(self, num): 48 | if num < 1: 49 | logging.debug('Must be a positive number') 50 | return None 51 | 52 | length = 0 53 | temp = self.head 54 | while temp: 55 | length += 1 56 | temp = temp.next 57 | 58 | if num > length: 59 | logging.debug('Input number cannot be larger than number of nodes') 60 | return None 61 | else: 62 | temp = self.head 63 | for x in range(length - num): 64 | temp = temp.next 65 | 66 | return temp.data 67 | 68 | def toList(self): 69 | n = self.head 70 | 71 | result = [] 72 | while(n): 73 | result.append(n.data) 74 | n = n.next 75 | return result 76 | 77 | 78 | class Test(unittest.TestCase): 79 | @classmethod 80 | def setUpClass(cls): 81 | cls.link = SinglyLinkedList() 82 | cls.link.addNode(-1) 83 | cls.link.addNode(-1) 84 | cls.link.addNode(4) 85 | cls.link.addNode(3) 86 | cls.link.addNode(4) 87 | cls.link.addNode(1) 88 | cls.link.addNode(20) 89 | cls.link.addNode(4) 90 | cls.link.addNode(4) 91 | 92 | @classmethod 93 | def tearDownClass(cls): 94 | logging.debug('---Teardown successful---') 95 | 96 | def test1(self): 97 | self.assertEqual(self.link.toList(), [ 98 | -1, 99 | -1, 100 | 4, 101 | 3, 102 | 4, 103 | 1, 104 | 20, 105 | 4, 106 | 4, 107 | ]) 108 | 109 | def test2(self): 110 | self.assertEqual(self.link.kthToLastElement(0), None) 111 | self.assertEqual(self.link.kthToLastElement(1), 4) 112 | self.assertEqual(self.link.kthToLastElement(2), 4) 113 | self.assertEqual(self.link.kthToLastElement(3), 20) 114 | self.assertEqual(self.link.kthToLastElement(9), -1) 115 | self.assertEqual(self.link.kthToLastElement(10), None) 116 | 117 | 118 | if __name__ == "__main__": 119 | unittest.main() 120 | -------------------------------------------------------------------------------- /Python/chapter02/p01_remove_dups/camilosalazar98.py: -------------------------------------------------------------------------------- 1 | """Romove Dups! Write code to remove duplicates from an unsorted linked list. 2 | FOLLOW UP 3 | How would you solve this problem if a temporary 4 | buffer is not allowed? Hints: #9, #40 5 | """ 6 | 7 | import unittest 8 | 9 | 10 | class Node: 11 | #Singly link list 12 | def __init__(self,data): 13 | self.data = data 14 | self.next = None 15 | 16 | class linklist: 17 | #linkList class 18 | def __init__(self): 19 | self.head = None 20 | 21 | def push(self,data): 22 | node = Node(data)# create a new node 23 | 24 | if self.head == None: #check to see if the head node is empty 25 | self.head = node # If its empty add it to the new node 26 | return 27 | #if the head of the linklist is filled 28 | 29 | current = self.head 30 | while current.next is not None:#Check the current postion is empty 31 | #Move to the next line if nothing is there 32 | current = current.next 33 | 34 | current.next = node #point self.head to a new node 35 | 36 | def length(self): 37 | #note the count doesn't start at zero 38 | cur = self.head 39 | counter = 0 40 | while cur is not None: 41 | counter+=1 42 | cur = cur.next 43 | return counter 44 | 45 | def toList(self): 46 | curr = self.head 47 | elem = [] 48 | 49 | while(curr != None): 50 | elem.append(curr.data) 51 | curr = curr.next 52 | return elem 53 | 54 | #1->2->3 55 | def remove_node(self,data): 56 | #1->2->3 57 | curr = self.head 58 | if curr is not None and curr.data == data: 59 | self.head = curr.next 60 | curr = None 61 | #look for the node that has the data we are looking for 62 | while curr is not None: 63 | if curr.data == data: 64 | break 65 | prev = curr 66 | curr = curr.next 67 | 68 | #if data isn't found just reutrn 69 | if(curr == None): 70 | return 71 | 72 | #allow us to unlink the nodes 73 | prev.next = curr.next 74 | curr = None 75 | 76 | def remove_dups(self): 77 | d_ = set()# create a set where only unquine element can be in 78 | curr = self.head 79 | while curr is not None: 80 | d_.add(curr.data)#add all the elements to the set 81 | curr = curr.next 82 | d_ = list(d_)#make theem in to a list 83 | self.head = None#clear the orignal link list 84 | for i in range(len(d_)): 85 | self.push(d_[i])#Use the element in the set a build a linklist with no dups 86 | 87 | 88 | 89 | 90 | 91 | 92 | class Test(unittest.TestCase): 93 | def test_remove_dups(self): 94 | llist = linklist() 95 | llist.push(2) 96 | llist.push(2) 97 | llist.push(2) 98 | llist.push(2) 99 | llist.push(2) 100 | self.assertEqual(llist.toList(), [2, 2, 2, 2, 2]) 101 | self.assertEqual(llist.length(), 5) 102 | #Testing 1->2->1->3->1 result should only be 1->2->3 103 | llist.remove_dups() 104 | self.assertEqual(llist.toList(), [2]) 105 | self.assertEqual(llist.length(), 1) 106 | 107 | 108 | if __name__ =='__main__': 109 | unittest.main() 110 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p05_oneaway/avc278.js: -------------------------------------------------------------------------------- 1 | // One Away: There are three types of edits that can be performed on strings: insert a character, remove a character, 2 | // or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away. 3 | 4 | const assert = require("assert"); 5 | 6 | /** 7 | * Check if the two strings are zero or one edits away 8 | * @param {string} A input string one to check against 9 | * @param {string} B input string two to check against 10 | * @return {boolean} whether the input strings can be edited zero or one ways to be equal 11 | * 12 | * There are two cases here: (1) A.length === B.length, and (2) A.length !== B.length. 13 | * For the first case, all we need to do is iterate through the strings and ensure their elements are equal, allowing 14 | * room for one misplaced character. Iterating through this takes O(N) time, without any additional space. 15 | * For the second case, we iterate through the different length strings, and perform almost identical comparisons. 16 | * Again, we allow a single misplaced character, or lack of character as we iterate through them. This also takes O(N) 17 | * time. For the sake of keeping things DRY, we find out which string is the shorter one and instantiate a copy of it, 18 | * as well as a copy of the long string. For this reason, we take additional O(N) space. If we wanted, we could perform 19 | * two while loops (a < A.length + 1 && b < B.length) as well as (a < A.length && b < B.length + 1), so we don't take 20 | * any additional space. 21 | * Runtime: O(N) 22 | * Space: O(N) 23 | * 24 | */ 25 | const oneAway = (A, B) => { 26 | if (Math.abs(A.length - B.length) > 1) return false; 27 | 28 | if (A.length === B.length) { 29 | let slack = true; 30 | for (let i = 0; i < A.length; i++) { 31 | if (A[i] === B[i]) continue; 32 | 33 | if (!slack) return false; 34 | slack = !slack; 35 | } 36 | return true; 37 | } 38 | 39 | const shortStr = A.length < B.length ? A : B; 40 | const longStr = A.length < B.length ? B : A; 41 | let sIdx = 0; 42 | let lIdx = 0; 43 | let slack = true; 44 | while (sIdx < shortStr.length + 1 && lIdx < longStr.length) { 45 | if (shortStr[sIdx] === longStr[lIdx]) { 46 | sIdx += 1; 47 | lIdx += 1; 48 | } else { 49 | if (!slack) return false; 50 | slack = !slack; 51 | lIdx += 1; 52 | } 53 | } 54 | return true; 55 | }; 56 | 57 | describe(module.filename, () => { 58 | it("should return true when input strings are equal.", () => { 59 | assert.ok(oneAway("tech", "tech")); 60 | }); 61 | it("should return true when input strings are of equal length, and replacing a character makes them equal.", () => { 62 | // replacing at the start of the string 63 | assert.ok(oneAway("sech", "tech")); 64 | assert.ok(oneAway("tech", "sech")); 65 | // replacing in the middle of the string 66 | assert.ok(oneAway("tdch", "tech")); 67 | assert.ok(oneAway("tech", "tdch")); 68 | // replacing at the end of the string 69 | assert.ok(oneAway("tecg", "tech")); 70 | assert.ok(oneAway("tech", "tecg")); 71 | }); 72 | it("should return true when input strings lengths are one apart and inserting/deleting a character makes them equal.", () => { 73 | assert.ok(oneAway("tech", "stech")); 74 | assert.ok(oneAway("tech", "ech")); 75 | }); 76 | it("should return false when the input strings are not zero/one edits away.", () => { 77 | assert.ok(!oneAway("tech", "stack")); 78 | assert.ok(!oneAway("tech", "techqueria")); 79 | }); 80 | }); 81 | -------------------------------------------------------------------------------- /JavaScript/chapter01/p07_rotate_matrix/avc278.js: -------------------------------------------------------------------------------- 1 | // Rotate Matrix: Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method 2 | // to rotate the image by 90 degrees. Can you do this in place? 3 | 4 | const assert = require("assert"); 5 | 6 | /** 7 | * Rotates the inputted 2d array by 90 degrees 8 | * @param {Array.} matrix input 2d array to rotate 9 | * @return {null} 10 | * 11 | * rotateMatrix is a void function since we don't actually return anything, but rather mutate the input array. 12 | * The trick here is to take the same knowledge of swapping two elements in an array with a temp to do it in place, 13 | * 14 | * const swap = (arr, i, j) => { 15 | * const temp = arr[i]; 16 | * arr[i] = arr[j]; 17 | * arr[j] = temp; 18 | * } 19 | * 20 | * and apply that to a 2d array. Instead of tracking i and j in one array, we keep track of i, j, M-1-i, and M-1-j. 21 | * As we iterate through the array, the positions that we swap shift by one until we finish rotating elements, layer by 22 | * layer. 23 | * 24 | * Runtime: O(N^2) 25 | * Space: O(1) 26 | * 27 | */ 28 | const rotateMatrix = (matrix) => { 29 | if (matrix.length < 2) return; 30 | 31 | const M = matrix.length; 32 | for (let i = 0; i < M / 2; i++) { 33 | for (let j = i; j < M - i - 1; j++) { 34 | const temp = matrix[i][j]; 35 | // swap i, j, M-1-i, M-1-j 36 | matrix[i][j] = matrix[j][M - 1 - i]; 37 | matrix[j][M - 1 - i] = matrix[M - 1 - i][M - 1 - j]; 38 | matrix[M - 1 - i][M - 1 - j] = matrix[M - 1 - j][i]; 39 | matrix[M - 1 - j][i] = temp; 40 | } 41 | } 42 | 43 | return; 44 | }; 45 | 46 | describe(module.filename, () => { 47 | it("should not affect a matrix of size 0", () => { 48 | const matrix = []; 49 | 50 | rotateMatrix(matrix); 51 | assert.deepEqual(matrix, []); 52 | }); 53 | it("should not affect a matrix of size 1", () => { 54 | const matrix = [5]; 55 | 56 | rotateMatrix(matrix); 57 | assert.deepEqual(matrix, [5]); 58 | }); 59 | it("should rotate a 2x2 matrix 90 degrees", () => { 60 | const matrix = [ 61 | [1, 2], 62 | [3, 4], 63 | ]; 64 | 65 | rotateMatrix(matrix); 66 | assert.deepEqual(matrix, [ 67 | [2, 4], 68 | [1, 3], 69 | ]); 70 | }); 71 | it("should rotate a 3x3 matrix 90 degrees", () => { 72 | const matrix = [ 73 | [1, 2, 3], 74 | [4, 5, 6], 75 | [7, 8, 9], 76 | ]; 77 | 78 | rotateMatrix(matrix); 79 | assert.deepEqual(matrix, [ 80 | [3, 6, 9], 81 | [2, 5, 8], 82 | [1, 4, 7], 83 | ]); 84 | }); 85 | it("should rotate a 4x4 matrix 90 degrees", () => { 86 | const matrix = [ 87 | [11, 12, 13, 14], 88 | [15, 16, 17, 18], 89 | [19, 20, 21, 22], 90 | [23, 24, 25, 26], 91 | ]; 92 | 93 | rotateMatrix(matrix); 94 | assert.deepEqual(matrix, [ 95 | [14, 18, 22, 26], 96 | [13, 17, 21, 25], 97 | [12, 16, 20, 24], 98 | [11, 15, 19, 23], 99 | ]); 100 | }); 101 | it("should bring the matrix to its original position after rotating 4 times.", () => { 102 | const matrix = [ 103 | [1, 2, 3], 104 | [4, 5, 6], 105 | [7, 8, 9], 106 | ]; 107 | 108 | rotateMatrix(matrix); 109 | rotateMatrix(matrix); 110 | rotateMatrix(matrix); 111 | rotateMatrix(matrix); 112 | assert.deepEqual(matrix, [ 113 | [1, 2, 3], 114 | [4, 5, 6], 115 | [7, 8, 9], 116 | ]); 117 | }); 118 | }); 119 | --------------------------------------------------------------------------------