Skip to main content

Posts

Programming Skills LeetCode Day 2, Solutions:

  1. Number of 1 bits Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). Solution Hamming Weight (roughly saying) is number of 1's string, or the digits sum of binary representation.  Example: 111 0 1 has hamming weight of 4                 678 0 1234 0 567 has hamming weight of 10.                    0000 has hamming weight of 0.         Code to solve this problem is:                     class Solution:               def hammingWeight(self, n: int) -> int:                 return ((bin(n).count('1')))      2. Subtract the Product and Sum of Digits of an Integer The problem goes like this: Given digits 123 is a integer. We have to first find the product of individual digits (i.e. 1 * 2 * 3) and then subtract to the sum of individual digits (i.e. 1 + 2 + 3). Which is 6 - 6 equals to zero.  Another example: 234 -> 2 * 3 * 4 = 24               2 + 3 + 4 = 9             24 - 9 = 15 Code to
Recent posts

Programming Skills LeetCode Day 1: Solutions

  Day 1 of LeetCode are 1. Count odd numbers in an interval range:     If we do not apply trick to this question, then we are going to fail. If we try testing each number one by one, it will take up a lot of memory and runtime. And henceforth, we fail. The trick is simple. i. Check to find if first number or last number is odd. If that's the case, we can do like this:               if (low%2 == 1 or high %2 == 1):                 return (high-low)//2 + 1 If you get confused, just take pen and paper and try it yourself. ii. If the above case is not satisfied:              else:                    return (high-low)//2       Complete Solution:             class Solution:              def countOdds(self,low:int, high:int)->int:                   if (low%2 == 1 or high%2 == 1):                        return (high-low)//2 + 1                    else:                   return (high-low)//2 2. Average Salary Excluding the Minimum and Maximum Salary     This is even more easy than prev

100 days of code with Saroj Bhattarai

A few days ago, I saw "100 days of code" talk on YouTube. A girl over there was telling how it changed her life (Link at the bottom of this blog). So I decided to give it a try. I had a habit of quitting everything before finishing it. For this and most of all, I was unproductive in Lockdown due to Corona Virus. I wanted to utilize this time. So I went to 100 days of code website .  I was motivated by the thought I wanted to do something, so I tweeted their tweet. Till that time I was unsure, what am I going to do? I just tweeted and wanted to figure it out what I would be doing.  I thought for half an hour. Reviewing all of my coding skills I decided to do Android Projects. I have successfully completed my day 1. It was fun because I did study a little study and knew some of Android prior to coding. Check out my GitHub . I have shared my thoughts on the log of Day0. You can also find my log over there.  I will write several other blogs what state am I at? How much did I impr

Web Scraping using Beautiful Soup (NEPSE LIVE DATA SCRAPING IN PYTHON)

from  bs4  import  BeautifulSoup import  requests import  csv source = requests.get( "http://nepalstock.com.np/" ).text soup = BeautifulSoup(source,  'lxml' ) csv_file =  open ( 'nepse.csv' , 'w' ) csv_writer = csv.writer(csv_file) csv_writer.writerow([ 'Symbol' ,  'Values' ,  'Total Traded amount' ]) marquee_tag = soup.find(      'div' ,  class_ = "col-xs-10 col-md-10 col-sm-12" ).marquee.b for  span_tag  in  marquee_tag( 'span' ):     span_tag.replace_with( '' ) for  img_tag  in  marquee_tag( 'img' ):     img_tag.replace_with( '' ) symbol_list = marquee_tag.text.split( '( )' ) all_symbols = [] short_name = [] per_share_value = [] total_traded_amount = [] a1 = [] a2 = [] a3 = [] for  symbol  in  symbol_list:     symbol_name = symbol.replace( u ' \xa0 ' ,  u '' )     all_symbols.append(symbol_name) all_

How to generate Golden Ratio by programming?

Golden ratio has been a fascinating topic for last more than 2400 years despite it does not have reliable evidence. Golden ratio is seen in the circle of Sunflower "flower". We can get golden ratio when we divide a line into two parts, the ratio of dividing the large part with small part is equal to the whole length divided by the longer part. For a programmer it will be more fascinating to know how to code the golden ratio. The most easy way to do this is by using Fibonacci Series. We can use python code like this. s = 0 a = 1 b = 1 for x in range(9999): c = a + b a = b print (c/b)  #This is the golden ratio b = c Golden ratio occurs in nature in different forms. Some of the examples are: 1. Flower Petals 2. Seed Heads 3. Pine Cones 4. Shells 5. Spiral galaxies 6. Hurricanes 7. Fingers 8. Animal Bodies 9. DNA molecules Thank you! A blog by Saroj Bhattarai

How to write PROLOG program for half adder?

In this problem, we are going to implement some logic (of course it is all about logic) that will run calculate the half adder sum and carry. We know has two inputs, and it will give sum and carry which looks like this: X            Y                 Sum                 Carry 0            0                  0                      0 0            1                  1                      0          1            0                  1                      0 1            1                  0                      1 So, to implement sum we need XOR gate and to implement Carry we need AND gate. Here is how we do it: xORGate(0,0,0). xORGate(0,0,0). xORGate(0,0,0). xORGate(0,0,0). aNDGate(0,0,0). aNDGate(0,0,0). aNDGate(0,0,0). aNDGate(0,0,0). halfadder(X,Y,S,C):-xORGate(X,Y,S), aNDGate(X,Y,C).  Now, you can run this code by giving the values of X and Y. like this: halfadder(1,1,S,C)   and the output will be S=0 C=1

Who am I?

Who am I? Am I really the same person a year ago? Are my perceptions changed about people? Don’t I look the world like other people use to see it? Who am I really? A student, a writer? I might. It’s kind of puzzling. Why I find my perceptions being changed for people whom I find very cool a year ago? Who am I when there is nobody to impress? From my endless talk you might think, I am the most talkative person, or from my level of humor (which most of you don’t get) you might think my talks are very big. Or from my silence (those who don’t know me), you might think I am shy or the meanest or the coldest guy alive. I am none of those!! Then who am I? Why am I even writing this? Am I writing this for me or for some other person, with hope, s/he will read this someday. Okay! I wrote for you and for me. I’m not writing this to exaggerate my feelings and I’m never been that sort of person.  Am I really not seeing the forest for the trees?  PS: Waiting for Feedback. Th