Skip to main content

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: 11101 has hamming weight of 4

                678012340567 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 solve this problem:

         class Solution:

    def subtractProductAndSum(self, n: int) -> int:

        finalSum = 0

        total = []

        product = 1

        for i in range (len(str(n))):

                s = n % 10                

                n = n // 10

                product = product * s

                finalSum = finalSum + s

        return (product-finalSum)

Note: This is my way of solving these problems. There are even more efficient and elegant way, feel free to share your ideas.

Comments

Popular posts from this blog

Basic guidelines for Chess opening.

If you are reading this blog, you probably know how the pieces move in the game of chess.  The player with the white pieces always moves first. The opening is generally considered complete when your rooks are connected. There are some basic principles to follow how to play efficiently. We should focus on the development of pieces. Here are top 10 opening rules for good chess opening: We must attack the center of board. So,  OPEN  with a  CENTER PAWN like  e4, d4. While ( e4 ,  d4 ,  e5 ,  d5 ) are generally regarded as the central squares, the same principle can sometimes be extended to the adjacent squares like ( c4 ,  c5 ,  d3 ,  d6 ,  e3 ,  e6 ,  f4 ,  f5 ).      DEVELOP pieces with  threats .  Always develop your Knights before Bishops . Develop knights to towards the center of board rather than side of board. When the knight is developed in the center, it could attack up to 8 squares.  The real reason to  develop Knights before Bishops  is that Knight are shorter- range pie

How to get GitHub Student Pack, Apply now

  Git is a version control system . G itHub is a web-based hosting service for version control using git . You can save all the projects on GitHub. GitHub has given some benefits for students. They recently are providing the student development pack for the students to give access to developers tools for free. Some of these real world tools are too expensive for a student to use. So, for those students who love the education, this could be just great! To get students developer pack, we are going to follow these steps: 1. First you need to make an account on GitHub. Go to this link https://www.github.com/ and sign up for free . 2. After making an account on GitHub, visit the link https://education.github.com/pack/ . 3.  Click on “ Get your pack ” button.  4.   Sign with GitHub with your username and password.  5.  Click on the button “ Yes, I’m a Student ”. 6.  Enter your name and verify your academic status. Add a photo of your identity card where it says “

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_