Python Bootcamp 100 Days of Code

Honda.putha

Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    You have fork it to you replit bro. You see there is button called fork in the upper right corner
    Ohh I saw it now.

    Thanks!

    #Day_1

    Completed the day 1.

    Things I have learned;

    1. Strings can be printed with '' or with "". You can use one type to cover the whole when you want to display the other type.
    e.g. print('He said "OMG"')
    output : He said "OMG"

    3. Indentation is important in Python.
    ------ Post added on Jun 30, 2021 at 9:01 AM
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    #Day_13
    Debugging: How to Find and Fix Errors in your Code (46min)

    මේ tool එක ගොඩක් වටිනව debug කරගන්න.
    http://www.pythontutor.com/visualize.html#mode=edit
    ------------------------------------------------------------------
    #Day_14
    Higher Lower Game Project (39min)

    day 14 එකත් කලා. මොකද අලුත් දෙයක් නෑ. project එකක් තියෙන නිසා. අනිත් දේ දවස් දෙකේම duration එකත් ගොඩක් අඩුයි. වෙලාව තිබ්බ. මම නම් තනියෙන්ම project එක කලා. කාටහරි අවුලක් ආවොත් අහන්න.
    -------------------------------------------------------------------

    5l4M3wK.jpg


    Beginner level එක අදින් ඉවරයි. මම නම් two days ahead ඉන්නෙ. මෙටික පුලුවන් කියන්නෙත් Python වලින් සෑහෙන්න වැඩක් කරගන්න පුලුවන්. ඇත්තටම ඉතුරු ටික ඕනෙ වෙලාවට google කරල හොයල අරන් කරන්නත් පුලුවන්. ගොඩක් Python course වල තියෙන්නෙත් මෙච්චරයි. තව එකම lesson එකයි අඩු classes and inheritance lesson එක Day 16. එතනට course එක complete කලා කියන්නෙ කොහොමත් python පුලුවන් කියන එක තමා.
    මොකද basic concepts දන්න නිසා ඉතුරුව ඕනෙ ඕනෙ වෙලාවට google කරකර ගොඩ දාගන්න පුලුවන්. හැබැ‍යි දෙයක් කරන්න යන කාලෙ වැඩියි. ඒ වගේම බොහොම පැටලුනු වියවුල් උන දැනුමක් එන්නෙ. ඒ විදියට ඉගෙන ගත්තාම problem එකකදි හොදම solution එක ඔලුවට එන්නෙ නෑ. මොකද අපි හරි පිලිවෙලකට ඉගෙන ගෙන නැති නිසා. ඒ නිසා මේකෙ ඉතුරු ටිකත් කලොත් ගොඩක් වටිනව.

    TIP
    මම course content බැලුවෙ 1.25 speed එකෙන්. ඒ වගේම වල code challenge solution එක බැලුවෙ 1.5 speed එකෙන්. day end එකේ motivational video එකත් හැමදාම බැලුව. ඒකත් වටිනව ඊලග දවසෙත් course එක කරන්න.
     
    Last edited:

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    #Day_2

    1. Acquire a particular character from a String
    print("Hellot"[0]

    2. To display a large number, but compiler will ignore it.
    print(123_454_444)

    3. Find the type
    print(type(variable)

    4. Division results a floating point number.

    5. Operator precedence
    ()
    **
    * /
    + -

    6. Floor division (x//y)

    7. f-string - to easily print any type of variable.
    print(f"My name is {name}, my age is {age}")

    :yes: :yes:

    TIP
    මම course content බැලුවෙ 1.25 speed එකෙන්. ඒ වගේම වල code challenge solution එක බැලුවෙ 1.5 speed එකෙන්. day end එකේ motivational video එකත් හැමදාම බැලුව. ඒකත් වටිනව ඊලග දවසෙත් course එක කරන්න.
    I'm doing that too. But I spent 2 and 1/2 hours to complete the day two even though it says 1 hr and 12 minutes.
    ------ Post added on Jul 1, 2021 at 6:35 AM
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    #Day_15
    Local Development Environment Setup & the Coffee Machine (1hr 6min)

    අද තමයි අපි Python අපේ PC එකට download කරල install කරගන්නෙ. මෙච්චර දවසක් repl එකෙන් නෙ වැඩ කලේ. මම නම් මේ ටික කලින්ම කරගෙන තිබ්බෙ. ඒ නිසා මුල් 30mins වගේ ඉතුරු උනා.

    TIP 1
    repl එකේ කරපු වැඩ ටික ඔක්කොම local pc එකට download කරගන්න. repl files ඔක්කොම තියෙන්නෙ එකම නමින්. repl name එකනෙ වෙනස්. download කරද්දි repl name එක වැඩක් නෑ අපිට. file name එකයි වැදගත්. ඔක්කොම files තියෙන්නෙ main.py, art.py නමින්. ඒ නිසා මේ files ටික ඔක්කොම එක පාර download කරගන්න එපා. එතකොට browser එකෙන් rename කරනව main(1).py, art(1).py. etc.. ඊලගට ඔය files වැඩ කරන්නෙ නෑ.

    එ නිසා folder එකක් හදල ඒ folder එක ඇතුලෙ lesson name එකෙන් folder එකක් හදල වෙන වෙනම ඒ folder ඇතුලට download කරගන්න. නැත්නම් එක day එකක files ටික download කරල ඒ files ටික අදාල lesson folder එකට drag n drop කරල ඊලග දවසෙ files download කරගන්න.

    TIP 2
    Folder හදද්දි Day name එකෙන් Folder හදන්න එපා. ඒක කිසිම වැඩක් නැති දෙයක්. මොකද පසූ කාලීනව code එකක් ආපහු ඕනෙ උනොත් අපිට මතක් වෙනව course එකේ මෙහෙම දෙයක් කලා ඒ lesson එකේ code එක තියෙනවනෙ කියල. ඊලගට day name එකෙන් Folder save කරල තිබ්බොත් අපිට මතක නෑ මොන day එකේද ඉගැන්නුවෙ කියල, ඔක්කොම හොය හොය බලන්න වෙනව. ඒ නිසා lesson name එකෙන් folder save කරගන්න. ඕනෙම නම් මුලට day name එකත් එක්ක දාගන. එතකොට folder ටිකත් ලස්සනට sort වෙනව.

    Code:
    day_01_variables
    day_02_string_manipulate
    day_03_control_flow

    54kSb5G.jpg


    මම Java වලින් Python ආපු නිසා PEP8 guides වලට file names නෑ. java rules වලට හදල තියෙන්නෙ. PEP8 rules දැනන් හිටියෙ නෑ මුලදි. ඔක්කොම lower case use කරන්න. word දෙකක් අතර underscore තියන්න.
    Ex - logical_operators.py
     

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    #Day_15
    Local Development Environment Setup & the Coffee Machine (1hr 6min)

    අද තමයි අපි Python අපේ PC එකට download කරල install කරගන්නෙ. මෙච්චර දවසක් repl එකෙන් නෙ වැඩ කලේ. මම නම් මේ ටික කලින්ම කරගෙන තිබ්බෙ. ඒ නිසා මුල් 30mins වගේ ඉතුරු උනා.

    TIP 1
    repl එකේ කරපු වැඩ ටික ඔක්කොම local pc එකට download කරගන්න. repl files ඔක්කොම තියෙන්නෙ එකම නමින්. repl name එකනෙ වෙනස්. download කරද්දි repl name එක වැඩක් නෑ අපිට. file name එකයි වැදගත්. ඔක්කොම files තියෙන්නෙ main.py, art.py නමින්. ඒ නිසා මේ files ටික ඔක්කොම එක පාර download කරගන්න එපා. එතකොට browser එකෙන් rename කරනව main(1).py, art(1).py. etc.. ඊලගට ඔය files වැඩ කරන්නෙ නෑ.

    එ නිසා folder එකක් හදල ඒ folder එක ඇතුලෙ lesson name එකෙන් folder එකක් හදල වෙන වෙනම ඒ folder ඇතුලට download කරගන්න. නැත්නම් එක day එකක files ටික download කරල ඒ files ටික අදාල lesson folder එකට drag n drop කරල ඊලග දවසෙ files download කරගන්න.

    TIP 2
    Folder හදද්දි Day name එකෙන් Folder හදන්න එපා. ඒක කිසිම වැඩක් නැති දෙයක්. මොකද පසූ කාලීනව code එකක් ආපහු ඕනෙ උනොත් අපිට මතක් වෙනව course එකේ මෙහෙම දෙයක් කලා ඒ lesson එකේ code එක තියෙනවනෙ කියල. ඊලගට day name එකෙන් Folder save කරල තිබ්බොත් අපිට මතක නෑ මොන day එකේද ඉගැන්නුවෙ කියල, ඔක්කොම හොය හොය බලන්න වෙනව. ඒ නිසා lesson name එකෙන් folder save කරගන්න. ඕනෙම නම් මුලට day name එකත් එක්ක දාගන. එතකොට folder ටිකත් ලස්සනට sort වෙනව.

    Code:
    day_01_variables
    day_02_string_manipulate
    day_03_control_flow

    54kSb5G.jpg


    මම Java වලින් Python ආපු නිසා PEP8 guides වලට file names නෑ. java rules වලට හදල තියෙන්නෙ. PEP8 rules දැනන් හිටියෙ නෑ මුලදි. ඔක්කොම lower case use කරන්න. word දෙකක් අතර underscore තියන්න.
    Ex - logical_operators.py
    ඇයි බන් ඔය ෆයිල් ඩවුන්ලෝඩ් කරගන්නෙ. ආයෙ බලන්නෙ නෑනෙ. මොලේට දාගන්න එකනෙ වෙන්න ඕනෙ මෙහෙම දේවල් තියෙනවා කියලා. එතකොට කෝඩ් එක ඕන නම් ගූගල් සර්ච් එකෙන් ගත්තැකි.
     
    • Like
    Reactions: MihiCherub

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    ඇයි බන් ඔය ෆයිල් ඩවුන්ලෝඩ් කරගන්නෙ. ආයෙ බලන්නෙ නෑනෙ. මොලේට දාගන්න එකනෙ වෙන්න ඕනෙ මෙහෙම දේවල් තියෙනවා කියලා. එතකොට කෝඩ් එක ඕන නම් ගූගල් සර්ච් එකෙන් ගත්තැකි.
    1. Google search කලාට එකම problem එකට solutions කීපයක් තියෙනව. ඒත්කොට ඒ ඔක්කොම solutions අපේ input එකත් එක්ක වැලීඩ් කියල ආයිත් ආයිත් බලන්න වෙනව. ඒත් දන්න solution code එක අපි already check කරල තියෙන නිසා input එකත් එක්ක ආයිආයි check කරන්න දෙයක් නෑ.

    2. සමහර problem වලට අපිට solution එකක් මතක් වෙන්නෙ නෑ. එහෙම problem එක google කරන්න හිතාගන්න බැරි අවස්තා එනව.
    (ගොඩක් efficient විදිය තමා solution එක google කරන එක. solution එක අපි දන්නව solution code එක ලියන්න ඕනෙ විදිය google කරනව) එහෙම solution එකක් හිතාගන්න බැරි තැන් වලදි lesson code review කරල solution එකක් හොයා ගන්න හරිම ලේසියි.

    උදාහරනයක් විදියට මේකෙම මම දාපු list 2ක් එකපාර කරන loop විදියට අදාල keyword එක මට මතක තිබ්බෙ නෑ. මම lesson files වලට ගිහින් තමා ඒ code එක copy කරගත්තෙ. ඒත් මන් google කලා නම් වෙන වෙන ක්‍රම අස්සෙ මට ඕනෙ ක්‍රමේ හොයා ගන්න වෙනව.

    https://stackoverflow.com/questions...mat-a-decimal-to-always-show-2-decimal-places
    මේ problem එක බලන්න. මේකෙ format කරන විදි හැටහුටහමාරක් තියෙනව. දැන් ඔය ඔක්කොම අපිට කියෝ කියෝ හොයනවට වඩා ලේසි නැද්ද Dr.Angela ගෙ tip calculator code එක open කරල පටස් ගාල format කරපු code කෑල්ල ගන්න එක. (Sublime එකේ code එක open වෙන්න 2 seconds max)
     

    hancok

    Well-known member
  • Aug 16, 2008
    36,592
    19,040
    113
    පිළි-ඇඳි-දොළ


    Ai ban mu mechchara wate gihin rachana liyanne. Mu harakekda nattan amaru gaanakda. Mata winadi 10ine giye mekata sarala code ekak gahanna

    Python:
    phonenumber = "3662277"
    words = ["foo","bar","baz","foobar","emo","cap","car","cat"]
    output = []
    
    dictionary = {"a":2,"b":2,"c":2,"d":3,"e":3,"f":3,"g":4,"h":4,"i":4,"j":5,"k":5,"l":5,"m":6,"n":6,"o":6,"p":7,"q":7,"r":7,"s":7,"t":8,"u":8,"v":8,"w":9,"x":9,"y":9,"z":9}
    
    def converter(word):
        number = ""
        for letters in word:
            number = number + str(dictionary[letters])
        return(number)
    
    for word in words:
        if converter(word) in phonenumber:
            output.append(word)
    
    print(output)
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha


    Ai ban mu mechchara wate gihin rachana liyanne. Mu harakekda nattan amaru gaanakda. Mata winadi 10ine giye mekata sarala code ekak gahanna

    Python:
    phonenumber = "3662277"
    words = ["foo","bar","baz","foobar","emo","cap","car","cat"]
    output = []
    
    dictionary = {"a":2,"b":2,"c":2,"d":3,"e":3,"f":3,"g":4,"h":4,"i":4,"j":5,"k":5,"l":5,"m":6,"n":6,"o":6,"p":7,"q":7,"r":7,"s":7,"t":8,"u":8,"v":8,"w":9,"x":9,"y":9,"z":9}
    
    def converter(word):
        number = ""
        for letters in word:
            number = number + str(dictionary[letters])
        return(number)
    
    for word in words:
        if converter(word) in phonenumber:
            output.append(word)
    
    print(output)

    uba me code karala thiyenne hardcoded solution ekak ne. input eka wenas weddi hardcoded solutions hariyanne na ban. crash wenawa.
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    Ai ban mu mechchara wate gihin rachana liyanne. Mu harakekda nattan amaru gaanakda. Mata winadi 10ine giye mekata sarala code ekak gahanna
    මම මේකට තව ටිකක් එකතු කරන්නම්. තාම Day 14 කරල නැති අය නම් මේව බලන්නවත් එපා තේරුමක් නෑ. demotivate වෙනව. අනික මේ problem එකට දෙන solution එක පට්ටම complex solution එකක්. interview එකේදි කියනව එච්චර දෙයක් බලාපොරොත්තු උනෙත් නෑ කියල. අඩුම ගානෙ මම වත් try කලේ නෑ. ඒතරමට අමාරුයි. එතකොට මේ @hancok 10min වලින්හැදුව කියන්නෙ මොකද්ද?

    ඒ ප්‍රශ්නෙ හරියට තේරුම් නොගැනීම. මෙතන අපිට output එක කොහොමහරි ගන්න එක නෙමේ challenge එක. algorithm එකක් ලියල output එක ගන්න එකයි challenge එක. ඒකට මම වෙන ප්‍රශ්නෙකින් පැහැදිලි කරන්නම්

    දැන් අපිට list එකක තියෙන value එකක index එක හොයන්න කියල code challenge එකක් interview එකකදි දෙනව.
    example
    Python:
    search_item = 33
    items = [10, 14, 19, 26, 27, 31, 33, 35, 42, 44]
    candidateල තුන් දෙනෙක් සහභාගි වෙනව.

    1st candidate @hancok වගේ කෙනා මෙහෙම ලියනව. ලියල කියනව තත්පර 10වත් ගියෙ නෑ කියල.
    Python:
    print(items.index(search_item))

    2nd candidate මෙහෙම algorithm එකක් ලියනව.
    Python:
    def linear_search(ar, search):
        for index in range(len(ar)):
            if ar[index] == search:
                return index
        return 'Trace Error'
    
    print(linear_search(items, search_item))

    3rd candidate මෙහෙම algorithm එකක් ලියනව.
    Python:
    def binary_search(ar, search):
        low = 0
        high = len(ar) - 1
    
        while True:
            # search not found
            if high < low:
                return 'Trace Error'
            mid = low + (high - low) // 2
    
            if ar[mid] == search:
                return mid
    
            elif ar[mid] < search:
                # upper bound
                low = mid + 1
            else:
                # lower bound
                high = mid - 1
    
    print(binary_search(items, search_item))

    දැන් interview එක අවසානෙදි තෝරගන්න කවුද. අනිවාර්යෙන්ම candidate 3. ඒකට හේතුව එයා තමා #හොදටම_කරල_තියෙන්නෙ.
    එහෙම වෙන්න හේතුව මෙතන ඉල්ලන්නෙ algorithm එකක් මිසක් කොහොමහරි output එක print කරන එක නෙමේ. ඒ නිසා candidate 1 @hancok පයින් ගහල එලවන්නෙ.

    2nd, 3rd candidate අතරෙන් 3rd එක්කෙනා හොද වෙන්න හේතුව මොකද්ද? ඒකට Big 'O' notation කියල එකක් තියෙනව. ඒකෙ සරල තේරුම අපි ප්‍රශ්නයකට විසදුමක් ලබා දීමේදි විසදුම ලැබෙන්න steps කීයක් යනවද කියන එක. ඒ කියන්නෙ ලියපු code lines ගාන නෙමේ. අවාසාන පිලිතුර ලැබෙන්න යන steps ගානයි. worst case scenario (දරුණුම දේ තමා) Big 'O' notation එකෙන් බලන්නෙ.

    2nd candidate ලියපු පිලිතුරේ කරන්නෙ list එකේ මුල ඉදන් අගට loop කරල බලනව index value එක search value එකත් එක්ක match වෙනවද කියල match වෙනව නම් index එක return කරනව.

    linear_search.gif


    දැන් මේ solution එකේ ප්‍රශ්නයක් තියෙනව. අපිට දෙන list එක එන්න එන්න ලොකු වැඩි වෙනකොට අපිට ඕනෙ value එක හොයන්න යන කාලය එන්න එන්න වැඩි වෙනව. ලොකු වට ප්‍රමානයක් ඕනෙ වෙනව. ඒකියන්නෙ ලොකු steps ප්‍රමානයක් ඕනෙ වෙනව. හිතන්න items 1000ක් තියෙන list එකක 998වෙනි index එකේ තමා අපි හොයන value එක තියෙන්නෙ . දැන් මේ search algorithm එක පාවිච්චි කලොත් loop එක වට 998ක් ගියාම තමා අපිට ඕනෙ index එක return වෙන්නෙ.
    Big 'O' notation එක බැලුවොත් O ( N )
    https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm

    Big-O-Notation-Linear-Algorithm.png


    3rd candidate ලියපු පිලිතුරේ කරන්නෙ Divide and conquer approach එක. main problem එක sub problem වලට divide කරගෙන ඒ පොඩි sub problems වෙන වෙනම විසදන එක,

    divide_and_conquer.jpg


    මේ algorithm එකේදි list එකේ මුල ඉදන් අගට බලන්නෙ නෑ. මුලින්ම list එකේ මැද්දෙන් පටන් අරන් search value එක තියෙන්න පුලුවන් පැත්ත හදුනාගෙන ඒ පැත්ත ආයිත් මැද්ද අරන් ඉස්සරහට යන algorithm එකක් තියෙන්නෙ.

    binary_search_1.jpg


    Big 'O' notation එක බැලුවොත් O(log n)
    https://www.tutorialspoint.com/data_structures_algorithms/binary_search_algorithm.htm


    Big-O-Notation-Logarithmic-Algorithm.png


    දැන් එතකොට 3rd candidate ගෙ algorithm එක තමා හොදම.
    තව search algorithm වර්ග තියෙනව. දැන් තාම course එකේ මුල් දවස් වල ඉන්න අය මේක කියවල demotivate වෙන්න එපා. මේව ටිකක් programming ඇඟට සෙට් උනාම කරල බලන්න ඕනෙ වැඩ. දැන්ම මේව කරන්න ගිහින් මේව නම් බෑ කියල ඔක්කොම අතාරින්න එපා.

    දවස් 14 complete අයට try කරල බලන්න හොද මේ වගේ තවත් problem එකක් තමා Largest prime factor problem එක.
    https://projecteuler.net/problem=3
    The prime factors of 13195 are 5, 7, 13 and 29.
    What is the largest prime factor of the number 600851475143 ?

    example දීල තියෙන 13195 number එකට algorithm code එකක් ලියාගන්න පුලුවන් උනත් අන්තිමට අහන number එකට algorithm එක වැඩ කරන්නෙ නැ කියල ඔයාලටම තේරෙයි. Big 'O' notation
     
    Last edited:

    hancok

    Well-known member
  • Aug 16, 2008
    36,592
    19,040
    113
    පිළි-ඇඳි-දොළ
    මම මේකට තව ටිකක් එකතු කරන්නම්. තාම Day 14 කරල නැති අය නම් මේව බලන්නවත් එපා තේරුමක් නෑ. demotivate වෙනව. අනික මේ problem එකට දෙන solution එක පට්ටම complex solution එකක්. interview එකේදි කියනව එච්චර දෙයක් බලාපොරොත්තු උනෙත් නෑ කියල. අඩුම ගානෙ මම වත් try කලේ නෑ. ඒතරමට අමාරුයි. එතකොට මේ @hancok 10min වලින්හැදුව කියන්නෙ මොකද්ද?

    ඒ ප්‍රශ්නෙ හරියට තේරුම් නොගැනීම. මෙතන අපිට output එක කොහොමහරි ගන්න එක නෙමේ challenge එක. algorithm එකක් ලියල output එක ගන්න එකයි challenge එක. ඒකට මම වෙන ප්‍රශ්නෙකින් පැහැදිලි කරන්නම්

    දැන් අපිට list එකක තියෙන value එකක index එක හොයන්න කියල code challenge එකක් interview එකකදි දෙනව.
    example
    Python:
    search_item = 33
    items = [10, 14, 19, 26, 27, 31, 33, 35, 42, 44]
    candidateල තුන් දෙනෙක් සහභාගි වෙනව.

    1st candidate @hancok වගේ කෙනා මෙහෙම ලියනව. ලියල කියනව තත්පර 10වත් ගියෙ නෑ කියල.
    Python:
    print(items.index(search_item))

    2nd candidate මෙහෙම algorithm එකක් ලියනව.
    Python:
    def linear_search(ar, search):
        for index in range(len(ar)):
            if ar[index] == search:
                return index
        return 'Trace Error'
    
    print(linear_search(items, search_item))

    3rd candidate මෙහෙම algorithm එකක් ලියනව.
    Python:
    def binary_search(ar, search):
        low = 0
        high = len(ar) - 1
    
        while True:
            # search not found
            if high < low:
                return 'Trace Error'
            mid = low + (high - low) // 2
    
            if ar[mid] == search:
                return mid
    
            elif ar[mid] < search:
                # upper bound
                low = mid + 1
            else:
                # lower bound
                high = mid - 1
    
    print(binary_search(items, search_item))

    දැන් interview එක අවසානෙදි තෝරගන්න කවුද. අනිවාර්යෙන්ම candidate 3. ඒකට හේතුව එයා තමා #හොදටම_කරල_තියෙන්නෙ.
    එහෙම වෙන්න හේතුව මෙතන ඉල්ලන්නෙ algorithm එකක් මිසක් කොහොමහරි output එක print කරන එක නෙමේ. ඒ නිසා candidate 1 @hancok පයින් ගහල එලවන්නෙ.

    2nd, 3rd candidate අතරෙන් 3rd එක්කෙනා හොද වෙන්න හේතුව මොකද්ද? ඒකට Big 'O' notation කියල එකක් තියෙනව. ඒකෙ සරල තේරුම අපි ප්‍රශ්නයකට විසදුමක් ලබා දීමේදි විසදුම ලැබෙන්න steps කීයක් යනවද කියන එක. ඒ කියන්නෙ ලියපු code lines ගාන නෙමේ. අවාසාන පිලිතුර ලැබෙන්න යන steps ගානයි. worst case scenario (දරුණුම දේ තමා) Big 'O' notation එකෙන් බලන්නෙ.

    2nd candidate ලියපු පිලිතුරේ කරන්නෙ list එකේ මුල ඉදන් අගට loop කරල බලනව index value එක search value එකත් එක්ක match වෙනවද කියල match වෙනව නම් index එක return කරනව.

    linear_search.gif


    දැන් මේ solution එකේ ප්‍රශ්නයක් තියෙනව. අපිට දෙන list එක එන්න එන්න ලොකු වැඩි වෙනකොට අපිට ඕනෙ value එක හොයන්න යන කාලය එන්න එන්න වැඩි වෙනව. ලොකු වට ප්‍රමානයක් ඕනෙ වෙනව. ඒකියන්නෙ ලොකු steps ප්‍රමානයක් ඕනෙ වෙනව. හිතන්න items 1000ක් තියෙන list එකක 998වෙනි index එකේ තමා අපි හොයන value එක තියෙන්නෙ . දැන් මේ search algorithm එක පාවිච්චි කලොත් loop එක වට 998ක් ගියාම තමා අපිට ඕනෙ index එක return වෙන්නෙ.
    Big 'O' notation එක බැලුවොත් O ( N )
    https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm

    Big-O-Notation-Linear-Algorithm.png


    3rd candidate ලියපු පිලිතුරේ කරන්නෙ Divide and conquer approach එක. main problem එක sub problem වලට divide කරගෙන ඒ පොඩි sub problems වෙන වෙනම විසදන එක,

    divide_and_conquer.jpg


    මේ algorithm එකේදි list එකේ මුල ඉදන් අගට බලන්නෙ නෑ. මුලින්ම list එකේ මැද්දෙන් පටන් අරන් search value එක තියෙන්න පුලුවන් පැත්ත හදුනාගෙන ඒ පැත්ත ආයිත් මැද්ද අරන් ඉස්සරහට යන algorithm එකක් තියෙන්නෙ.

    binary_search_1.jpg


    Big 'O' notation එක බැලුවොත් O(log n)
    https://www.tutorialspoint.com/data_structures_algorithms/binary_search_algorithm.htm


    Big-O-Notation-Logarithmic-Algorithm.png


    දැන් එතකොට 3rd candidate ගෙ algorithm එක තමා හොදම.
    තව search algorithm වර්ග තියෙනව. දැන් තාම course එකේ මුල් දවස් වල ඉන්න අය මේක කියවල demotivate වෙන්න එපා. මේව ටිකක් programming ඇඟට සෙට් උනාම කරල බලන්න ඕනෙ වැඩ. දැන්ම මේව කරන්න ගිහින් මේව නම් බෑ කියල ඔක්කොම අතාරින්න එපා.

    දවස් 14 complete අයට try කරල බලන්න හොද මේ වගේ තවත් problem එකක් තමා Largest prime factor problem එක.
    https://projecteuler.net/problem=3


    example දීල තියෙන 13195 number එකට algorithm code එකක් ලියාගන්න පුලුවන් උනත් අන්තිමට අහන number එකට algorithm එක වැඩ කරන්නෙ නැ කියල ඔයාලටම තේරෙයි. Big 'O' notation
    Mage solution eke awula thiyanne

    If this in that:

    Kotaseda?

    Youtube eke ekaa phone number eka kaali wlaata kada kada balanna gihin anagannawane. Mama kale mehemai

    Deepu wachana mulinma map kqragana iita adaala ilakkam version eka gannawa. Itq passe e ilakkam bmversion eka phone number eke kohe hari thiyanawada balanawa. - kohoma kalath lesima widiha ookane

    Oke dictionary ekak use karala deepu akurata adala value eka gatta ekada awula nattan (time efficiency ekka)

    Mama dapu code eka ona input set ekakata wada neda habai
     

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    මම මේකට තව ටිකක් එකතු කරන්නම්. තාම Day 14 කරල නැති අය නම් මේව බලන්නවත් එපා තේරුමක් නෑ. demotivate වෙනව. අනික මේ problem එකට දෙන solution එක පට්ටම complex solution එකක්. interview එකේදි කියනව එච්චර දෙයක් බලාපොරොත්තු උනෙත් නෑ කියල. අඩුම ගානෙ මම වත් try කලේ නෑ. ඒතරමට අමාරුයි. එතකොට මේ @hancok 10min වලින්හැදුව කියන්නෙ මොකද්ද?

    ඒ ප්‍රශ්නෙ හරියට තේරුම් නොගැනීම. මෙතන අපිට output එක කොහොමහරි ගන්න එක නෙමේ challenge එක. algorithm එකක් ලියල output එක ගන්න එකයි challenge එක. ඒකට මම වෙන ප්‍රශ්නෙකින් පැහැදිලි කරන්නම්

    දැන් අපිට list එකක තියෙන value එකක index එක හොයන්න කියල code challenge එකක් interview එකකදි දෙනව.
    example
    Python:
    search_item = 33
    items = [10, 14, 19, 26, 27, 31, 33, 35, 42, 44]
    candidateල තුන් දෙනෙක් සහභාගි වෙනව.

    1st candidate @hancok වගේ කෙනා මෙහෙම ලියනව. ලියල කියනව තත්පර 10වත් ගියෙ නෑ කියල.
    Python:
    print(items.index(search_item))

    2nd candidate මෙහෙම algorithm එකක් ලියනව.
    Python:
    def linear_search(ar, search):
        for index in range(len(ar)):
            if ar[index] == search:
                return index
        return 'Trace Error'
    
    print(linear_search(items, search_item))

    3rd candidate මෙහෙම algorithm එකක් ලියනව.
    Python:
    def binary_search(ar, search):
        low = 0
        high = len(ar) - 1
    
        while True:
            # search not found
            if high < low:
                return 'Trace Error'
            mid = low + (high - low) // 2
    
            if ar[mid] == search:
                return mid
    
            elif ar[mid] < search:
                # upper bound
                low = mid + 1
            else:
                # lower bound
                high = mid - 1
    
    print(binary_search(items, search_item))

    දැන් interview එක අවසානෙදි තෝරගන්න කවුද. අනිවාර්යෙන්ම candidate 3. ඒකට හේතුව එයා තමා #හොදටම_කරල_තියෙන්නෙ.
    එහෙම වෙන්න හේතුව මෙතන ඉල්ලන්නෙ algorithm එකක් මිසක් කොහොමහරි output එක print කරන එක නෙමේ. ඒ නිසා candidate 1 @hancok පයින් ගහල එලවන්නෙ.

    2nd, 3rd candidate අතරෙන් 3rd එක්කෙනා හොද වෙන්න හේතුව මොකද්ද? ඒකට Big 'O' notation කියල එකක් තියෙනව. ඒකෙ සරල තේරුම අපි ප්‍රශ්නයකට විසදුමක් ලබා දීමේදි විසදුම ලැබෙන්න steps කීයක් යනවද කියන එක. ඒ කියන්නෙ ලියපු code lines ගාන නෙමේ. අවාසාන පිලිතුර ලැබෙන්න යන steps ගානයි. worst case scenario (දරුණුම දේ තමා) Big 'O' notation එකෙන් බලන්නෙ.

    2nd candidate ලියපු පිලිතුරේ කරන්නෙ list එකේ මුල ඉදන් අගට loop කරල බලනව index value එක search value එකත් එක්ක match වෙනවද කියල match වෙනව නම් index එක return කරනව.

    linear_search.gif


    දැන් මේ solution එකේ ප්‍රශ්නයක් තියෙනව. අපිට දෙන list එක එන්න එන්න ලොකු වැඩි වෙනකොට අපිට ඕනෙ value එක හොයන්න යන කාලය එන්න එන්න වැඩි වෙනව. ලොකු වට ප්‍රමානයක් ඕනෙ වෙනව. ඒකියන්නෙ ලොකු steps ප්‍රමානයක් ඕනෙ වෙනව. හිතන්න items 1000ක් තියෙන list එකක 998වෙනි index එකේ තමා අපි හොයන value එක තියෙන්නෙ . දැන් මේ search algorithm එක පාවිච්චි කලොත් loop එක වට 998ක් ගියාම තමා අපිට ඕනෙ index එක return වෙන්නෙ.
    Big 'O' notation එක බැලුවොත් O ( N )
    https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm

    Big-O-Notation-Linear-Algorithm.png


    3rd candidate ලියපු පිලිතුරේ කරන්නෙ Divide and conquer approach එක. main problem එක sub problem වලට divide කරගෙන ඒ පොඩි sub problems වෙන වෙනම විසදන එක,

    divide_and_conquer.jpg


    මේ algorithm එකේදි list එකේ මුල ඉදන් අගට බලන්නෙ නෑ. මුලින්ම list එකේ මැද්දෙන් පටන් අරන් search value එක තියෙන්න පුලුවන් පැත්ත හදුනාගෙන ඒ පැත්ත ආයිත් මැද්ද අරන් ඉස්සරහට යන algorithm එකක් තියෙන්නෙ.

    binary_search_1.jpg


    Big 'O' notation එක බැලුවොත් O(log n)
    https://www.tutorialspoint.com/data_structures_algorithms/binary_search_algorithm.htm


    Big-O-Notation-Logarithmic-Algorithm.png


    දැන් එතකොට 3rd candidate ගෙ algorithm එක තමා හොදම.
    තව search algorithm වර්ග තියෙනව. දැන් තාම course එකේ මුල් දවස් වල ඉන්න අය මේක කියවල demotivate වෙන්න එපා. මේව ටිකක් programming ඇඟට සෙට් උනාම කරල බලන්න ඕනෙ වැඩ. දැන්ම මේව කරන්න ගිහින් මේව නම් බෑ කියල ඔක්කොම අතාරින්න එපා.

    දවස් 14 complete අයට try කරල බලන්න හොද මේ වගේ තවත් problem එකක් තමා Largest prime factor problem එක.
    https://projecteuler.net/problem=3


    example දීල තියෙන 13195 number එකට algorithm code එකක් ලියාගන්න පුලුවන් උනත් අන්තිමට අහන number එකට algorithm එක වැඩ කරන්නෙ නැ කියල ඔයාලටම තේරෙයි. Big 'O' notation
    Good explanation muchn!
     

    hancok

    Well-known member
  • Aug 16, 2008
    36,592
    19,040
    113
    පිළි-ඇඳි-දොළ


    Ai ban mu mechchara wate gihin rachana liyanne. Mu harakekda nattan amaru gaanakda. Mata winadi 10ine giye mekata sarala code ekak gahanna

    Python:
    phonenumber = "3662277"
    words = ["foo","bar","baz","foobar","emo","cap","car","cat"]
    output = []
    
    dictionary = {"a":2,"b":2,"c":2,"d":3,"e":3,"f":3,"g":4,"h":4,"i":4,"j":5,"k":5,"l":5,"m":6,"n":6,"o":6,"p":7,"q":7,"r":7,"s":7,"t":8,"u":8,"v":8,"w":9,"x":9,"y":9,"z":9}
    
    def converter(word):
        number = ""
        for letters in word:
            number = number + str(dictionary[letters])
        return(number)
    
    for word in words:
        if converter(word) in phonenumber:
            output.append(word)
    
    print(output)

    Menna meka specifically balala kiyapanko ban mage eke mokakda adupaduwa kiyala. Mage solution eka prashne dakka gaman ona ballekta senikawa therenawa but e solution eka brute force wage non optimized solution ekak kiyala hamoma ignore karala wena efficient approach hoyanawa wage line ekakda thiyanne. Nathuwa meka amaru ai kiyalanam mata therenne na hena lesiyine.
    Adala wachana wala equivalent ilakkam set eka gatta, eeka maha string eke thiyanawada baluwa

    if converter(word) in phonenumber:

    Kiyana kallada awul. Meken kohomada search wenne. Hena welawak yana widihatada.

    Nattan dictionary[letters]. Kiyana ekada awul. Eekath hena welaa gannawada.

    Nattan Adala wachana wala equivalent ilakkam set eka gatta, eeka maha string eke thiyanawada baluwa kiyana overall approach eke adupaduwakda. E approach ekata wada straightforward ekak penne nane mata
    @MihiCherub @BruceWayne92 @Mr.Thor

    මම මේකට තව ටිකක් එකතු කරන්නම්. තාම Day 14 කරල නැති අය නම් මේව බලන්නවත් එපා තේරුමක් නෑ. demotivate වෙනව. අනික මේ problem එකට දෙන solution එක පට්ටම complex solution එකක්. interview එකේදි කියනව එච්චර දෙයක් බලාපොරොත්තු උනෙත් නෑ කියල. අඩුම ගානෙ මම වත් try කලේ නෑ. ඒතරමට අමාරුයි. එතකොට මේ @hancok 10min වලින්හැදුව කියන්නෙ මොකද්ද?

    ඒ ප්‍රශ්නෙ හරියට තේරුම් නොගැනීම. මෙතන අපිට output එක කොහොමහරි ගන්න එක නෙමේ challenge එක. algorithm එකක් ලියල output එක ගන්න එකයි challenge එක. ඒකට මම වෙන ප්‍රශ්නෙකින් පැහැදිලි කරන්නම්

    දැන් අපිට list එකක තියෙන value එකක index එක හොයන්න කියල code challenge එකක් interview එකකදි දෙනව.
    example
    Python:
    search_item = 33
    items = [10, 14, 19, 26, 27, 31, 33, 35, 42, 44]
    candidateල තුන් දෙනෙක් සහභාගි වෙනව.

    1st candidate @hancok වගේ කෙනා මෙහෙම ලියනව. ලියල කියනව තත්පර 10වත් ගියෙ නෑ කියල.
    Python:
    print(items.index(search_item))

    2nd candidate මෙහෙම algorithm එකක් ලියනව.
    Python:
    def linear_search(ar, search):
        for index in range(len(ar)):
            if ar[index] == search:
                return index
        return 'Trace Error'
    
    print(linear_search(items, search_item))

    3rd candidate මෙහෙම algorithm එකක් ලියනව.
    Python:
    def binary_search(ar, search):
        low = 0
        high = len(ar) - 1
    
        while True:
            # search not found
            if high < low:
                return 'Trace Error'
            mid = low + (high - low) // 2
    
            if ar[mid] == search:
                return mid
    
            elif ar[mid] < search:
                # upper bound
                low = mid + 1
            else:
                # lower bound
                high = mid - 1
    
    print(binary_search(items, search_item))

    දැන් interview එක අවසානෙදි තෝරගන්න කවුද. අනිවාර්යෙන්ම candidate 3. ඒකට හේතුව එයා තමා #හොදටම_කරල_තියෙන්නෙ.
    එහෙම වෙන්න හේතුව මෙතන ඉල්ලන්නෙ algorithm එකක් මිසක් කොහොමහරි output එක print කරන එක නෙමේ. ඒ නිසා candidate 1 @hancok පයින් ගහල එලවන්නෙ.

    2nd, 3rd candidate අතරෙන් 3rd එක්කෙනා හොද වෙන්න හේතුව මොකද්ද? ඒකට Big 'O' notation කියල එකක් තියෙනව. ඒකෙ සරල තේරුම අපි ප්‍රශ්නයකට විසදුමක් ලබා දීමේදි විසදුම ලැබෙන්න steps කීයක් යනවද කියන එක. ඒ කියන්නෙ ලියපු code lines ගාන නෙමේ. අවාසාන පිලිතුර ලැබෙන්න යන steps ගානයි. worst case scenario (දරුණුම දේ තමා) Big 'O' notation එකෙන් බලන්නෙ.

    2nd candidate ලියපු පිලිතුරේ කරන්නෙ list එකේ මුල ඉදන් අගට loop කරල බලනව index value එක search value එකත් එක්ක match වෙනවද කියල match වෙනව නම් index එක return කරනව.

    linear_search.gif


    දැන් මේ solution එකේ ප්‍රශ්නයක් තියෙනව. අපිට දෙන list එක එන්න එන්න ලොකු වැඩි වෙනකොට අපිට ඕනෙ value එක හොයන්න යන කාලය එන්න එන්න වැඩි වෙනව. ලොකු වට ප්‍රමානයක් ඕනෙ වෙනව. ඒකියන්නෙ ලොකු steps ප්‍රමානයක් ඕනෙ වෙනව. හිතන්න items 1000ක් තියෙන list එකක 998වෙනි index එකේ තමා අපි හොයන value එක තියෙන්නෙ . දැන් මේ search algorithm එක පාවිච්චි කලොත් loop එක වට 998ක් ගියාම තමා අපිට ඕනෙ index එක return වෙන්නෙ.
    Big 'O' notation එක බැලුවොත් O ( N )
    https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm

    Big-O-Notation-Linear-Algorithm.png


    3rd candidate ලියපු පිලිතුරේ කරන්නෙ Divide and conquer approach එක. main problem එක sub problem වලට divide කරගෙන ඒ පොඩි sub problems වෙන වෙනම විසදන එක,

    divide_and_conquer.jpg


    මේ algorithm එකේදි list එකේ මුල ඉදන් අගට බලන්නෙ නෑ. මුලින්ම list එකේ මැද්දෙන් පටන් අරන් search value එක තියෙන්න පුලුවන් පැත්ත හදුනාගෙන ඒ පැත්ත ආයිත් මැද්ද අරන් ඉස්සරහට යන algorithm එකක් තියෙන්නෙ.

    binary_search_1.jpg


    Big 'O' notation එක බැලුවොත් O(log n)
    https://www.tutorialspoint.com/data_structures_algorithms/binary_search_algorithm.htm


    Big-O-Notation-Logarithmic-Algorithm.png


    දැන් එතකොට 3rd candidate ගෙ algorithm එක තමා හොදම.
    තව search algorithm වර්ග තියෙනව. දැන් තාම course එකේ මුල් දවස් වල ඉන්න අය මේක කියවල demotivate වෙන්න එපා. මේව ටිකක් programming ඇඟට සෙට් උනාම කරල බලන්න ඕනෙ වැඩ. දැන්ම මේව කරන්න ගිහින් මේව නම් බෑ කියල ඔක්කොම අතාරින්න එපා.

    දවස් 14 complete අයට try කරල බලන්න හොද මේ වගේ තවත් problem එකක් තමා Largest prime factor problem එක.
    https://projecteuler.net/problem=3


    example දීල තියෙන 13195 number එකට algorithm code එකක් ලියාගන්න පුලුවන් උනත් අන්තිමට අහන number එකට algorithm එක වැඩ කරන්නෙ නැ කියල ඔයාලටම තේරෙයි. Big 'O' notation
    Oya project eular q3 eke karanna thiyanne oya maha ilakkame wargamulaya wenakan prime list eka hadala maha ilakkama e prime list eken bedan yanna neda aga idan mulata. Ita wada adu kaalayak yana kramayak thiyanawada
    ------ Post added on Jul 2, 2021 at 8:59 PM