Thursday, July 21, 2016

Python If-Else - Hacker Rank Solution

Python If-Else - Hacker Rank Solution

Task 
Given an integer, , perform the following conditional actions:
  • If  is odd, print Weird
  • If  is even and in the inclusive range of  to , print Not Weird
  • If  is even and in the inclusive range of  to , print Weird
  • If  is even and greater than , print Not Weird
Input Format
A single line containing a positive integer, .
Constraints
Output Format
Print Weird if the number is weird; otherwise, print Not Weird.
Sample Input 0
3
Sample Output 0
Weird
Sample Input 1
24
Sample Output 1
Not Weird
Explanation
Sample Case 0:  
 is odd and odd numbers are weird, so we print Weird.
Sample Case 1:  
 and  is even, so it isn't weird. Thus, we print Not Weird.

Python If-Else - Hacker Rank Solution

Problem Setter's code :
Python 2
n = int(raw_input())
if n % 2 == 1:
    print "Weird"
elif n % 2 == 0 and 2 <= n <= 5:
    print "Not Weird"
elif n % 2 == 0 and 6 <= n <= 20:
    print "Weird"
else:
    print "Not Weird"
Python 3
n = int(input())
if n % 2 == 1:
    print("Weird")
elif n % 2 == 0 and 2 <= n <= 5:
    print("Not Weird")
elif n % 2 == 0 and 6 <= n <= 20:
    print("Weird")
else:
    print("Not Weird")

63 comments:

  1. if N %2 != 0 or 5<N<21:
    print ("Weird")
    else:
    print ("Not Weird")

    ReplyDelete
  2. n = int(raw_input())
    if n%2==0 and ((2 <= n <=5) or (n>20)):
    print("Not Weird")
    if (n%2==1 or (n%2==0 and (6 <= n <=20))):
    print("Weird")

    ReplyDelete
    Replies
    1. This program is sowing compilation error.
      Correct program is mentioned below
      n=int(input())
      if((n%2!=0)or(n%2==0 and 6<=n<=20)):
      print("Weird")
      elif((n%2==0 and 2<=n<=5)or(n%2==0 and n>20)):
      print("Not Weird")

      Delete
    2. Y we hve to "n=int(input())"

      Delete
    3. without it you can't print the value

      Delete
    4. what is wrong wit my code ?
      n=int(input())
      if n%2!==0 or (620:
      print("Not Weird")

      Delete
    5. n=int(input())
      if n % 2!= 0 or 620:
      print("Not Weird")

      Above is write way

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. n=int(input("enter your odd or even number: "))
    if n%2==1:
    print("weird")
    elif n%2==0 and 2<=n<=5:
    print("not weird")
    elif n%2==0 and 6<=n<=20:
    print("weird")
    else:
    print("not weird")

    ReplyDelete
  5. Why its showing weird for n=24

    ReplyDelete
  6. Thanks a lot for this information. I loves to read this blog.

    ReplyDelete





  7. #what is wrong with this code ?
    n=input()
    m=int(n)
    if m%2!=0:
    print("Weird")
    elif m%2==0:
    if(2<=m<=5):
    print("Not Weird")
    elif m%2==0:
    if (6<=m<=20):
    print("Weird")
    elif m%2==0:
    if (m>20):
    print("Not Weird")

    ReplyDelete
    Replies
    1. Bro last else varanum bro apatha correct

      Delete
    2. your code may be correct but
      why u take modulous of 2 more than one time .

      Delete
  8. #whats wrong with my code please help me out..!! Thanks in Advance!!
    n = int(input("enter odd or even number"))
    if n % 2 == 1:
    print("n is weird")
    else:
    if n >= 2 and n <= 5:
    print("n is not weird")
    elif n % 2 == 0 and 6 <= n <= 20:
    print("n is weird")
    elif n >= 20:
    print("n is not weird")

    ReplyDelete
    Replies
    1. n = int(input())
      if n % 2 == 1:
      print("Weird")
      else:
      if n >= 2 and n <= 5:
      print("Not Weird")
      elif n % 2 == 0 and 6 <= n <= 20:
      print("Weird")
      elif n >= 20:
      print("Not Weird")

      Delete
  9. They have imported re i.e, regular expression module so that must have something to do with the code

    ReplyDelete
    Replies
    1. yes you are right!!! but do you have any idea because if I remove than last__name__ = __ main__ this solution works but I don't know what to do now.

      Delete
  10. Why this code is not run?
    n = int(input())
    if n % 2 == 1:
    print("Weird")
    elif n % 2 == 0 and 2 <= n <= 5:
    print("Not Weird")
    elif n % 2 == 0 and 6 <= n <= 20:
    print("Weird")
    else:
    print("Not Weird")

    ReplyDelete
    Replies
    1. Im having the same issue with this

      Delete
    2. My code is same as that code but it showing indentation error. please tell me what is the fault in this code.

      Delete
  11. if n%2!=0:
    print("Weird")
    elif 2<=n<=5:
    print("Not Weird")
    elif 6<=n<=20:
    print("Weird")
    elif n>20:
    print("Not Weird")

    ReplyDelete
  12. Please remove that default if statement.It'll work then.

    ReplyDelete
  13. Can anyone check why 1 test case out of 8 is failing.

    def calculateGrade(students_marks):
    arr =students_marks
    result =[]

    for row in arr:
    avg=0
    number=0

    for col in row:

    number=number+col
    #print(number)
    avg=number/len(row)

    if avg >=90:
    result.append('A+')
    elif avg >=80 and avg <90:
    result.append('A')
    elif avg >=70 and avg <80:
    result.append('B')
    elif avg>=60 and avg <70:
    result.append('C')
    elif avg>50 and avg<60:
    result.append('D')
    else:
    result.append('F')

    return result

    ReplyDelete
  14. n = int(input())

    if (n%2 == 1):
    print("Weird")
    elif (n%2 ==0) and ((n >= 2) and (n <= 5)):
    print("Not Weird")
    elif (n%2 ==0) and ((n <= 6) and (n <= 20)):
    print("Weird")
    else:
    print("Not Weird")

    ReplyDelete
  15. i =int(input())
    if(i % 2)==0:
    if(2 <= i <= 5):
    print("Not Weird")
    if( 6<= i <= 20):
    print("Weird")
    if( 20 <=i):
    print("Not Weird")


    else:
    print("Weird")
    there is one test case wrong
    what is it

    ReplyDelete
  16. what is wrong in this code and also tell the other ways to assign the inclusive range
    if __name__ == '__main__':
    n = int(input().strip())
    if(n%2!=0):
    print('Weird')
    elif(n%2==0, n in range(2,5)):
    print('Not Weird')
    elif(n%2==0, n in range(6,20)):
    print('Weird')
    else:
    print('Not Weird')

    ReplyDelete
  17. n = int(input())
    if n % 2 !=0:
    print("Weird")
    else:
    if n % 2==0 and n>=2 and n>=5:
    print("Not Weird")
    elif n % 2==0 and n>=6 and n>=20:
    print("Weird")
    elif n>20 :
    print("Not Weird")

    ReplyDelete
  18. n = input (" ")

    if n % 2 == 1 and ((520) or (n==1)):
    print "Weird"
    else:
    if n >= 2 and n <= 5:
    print "Not Weird"
    elif n >= 6 and n <= 20:
    print "Weird"
    elif n > 20:
    print "Not Weird"

    ReplyDelete
    Replies
    1. n = input (" ")

      if n % 2 == 1 and ((520) or (n==1)):
      print "Weird"
      else:
      if n >= 2 and n <= 5:
      print "Not Weird"
      elif n >= 6 and n <= 20:
      print "Weird"
      elif n > 20:
      print "Not Weird"

      Delete
  19. n=int(input().strip())
    if((n%2!=0)or(n%2==0 and 6<=n<=20)):
    print("Weird")
    elif((n%2==0 and (2<=n<=5)or n>20)):
    print("Not Weird")

    ReplyDelete
  20. Why this code gives error???

    n = int(input().strip())
    if n % 2 == 1:
    print("Weird")
    else:
    if n in range(2,5) or n >20:
    print("Not Wierd")
    elif n in range(6,20):
    print("Weird")

    ReplyDelete
  21. n = int(input())
    if n % 2 == 0 and n < 5 or n > 20 : #true
    print("Not Weird")
    if n % 2 != 0 or n % 2 == 0 and n > 5 and n < 20 : #true
    print("Weird")

    27 gives both answers - can anyone assist?

    ReplyDelete
  22. if n%2!=0:
    print('Weird')
    elif n%2==0 and n in range(2,5):
    print('Not Weird')
    elif n%2==0 and n in range(6,21):
    print('Weird')
    else:
    print('Not Weird')

    ReplyDelete
  23. a=int(input())
    if a%2==0:
    if 1<a<6:
    print("Not Weird")
    elif 5<a<21:
    print("Weird")
    else:
    print("Not Weird")
    else:
    print("Weird")

    ReplyDelete
  24. # me ravi(akshay) gupta in linkedin
    a=int(input())
    if a%2==0:
    if 1<a<6:
    print("Not Weird")
    elif 5<a<21:
    print("Weird")
    else:
    print("Not Weird")
    else:
    print("Weird")

    ReplyDelete
  25. if n%2!=0:
    print('Weird')
    elif n%2==0 and 2 <= n >=5:
    print('Not Weird')
    elif n%2==0 and 6 <=n >=20:
    print('Weird')
    else:
    print('Weird')

    ReplyDelete
  26. n=int(input())
    if n%2 == 1:
    print("Weird")
    elif n%2 == 0 and 2<= n <=5:
    print("Not Weird")
    elif n%2 == 0 and 6<= n <=20:
    print("Weird")
    else:
    print("Not Weird ")

    ReplyDelete
  27. n=int(input())
    if n%2 == 1:
    print("Weird")
    elif n%2 == 0 and 2<= n =>5:
    print("Not Weird")
    elif n%2 == 0 and 6<= n =>20:
    print("Weird")
    else:
    print("Not Weird ")

    ReplyDelete
  28. n=int(input())
    if(n%2==0):
    if(n in range(2,6)): print("Not Weird")
    if(n in range(6,21)):print("Weird")
    if(n>20):print("Not Weird")
    else:print("Weird")

    ReplyDelete
  29. print("enter a no")
    a=int(input())

    n=a%2
    if n!=0 or n==0 and 6 <= a <= 20:
    print("weird")
    else:
    print("not weird")

    ReplyDelete
  30. n = int(input("Enter any number/n"))
    if n % 2 == 1:
    print("Weird")
    elif 2 <= n <= 5:
    print("Not Weird")
    elif 6 <= n <= 20:
    print("Weird")
    else:
    print("Not Weird")


    what is wrong with this code
    its showing correct output but hackerrank is not accepting it

    ReplyDelete
  31. if __name__ == '__main__':
    n = int(input().strip())
    if(n%2!=0):
    print("weird")
    elif(n%2==0):
    if(n in range(2,5)):
    print("not weird")
    elif(n in range(6,20)):
    print(" weird")
    if(n>20):
    print("not weird")

    ReplyDelete
    Replies
    1. only test case 0,4 is passed. what is the problem for other test case

      Delete
  32. i = int(input())
    if i%2!=0:
    print('Weird')
    elif i%2==0 and i in range(2,6):
    print('Not Weird')
    elif i%2==0 and i in range(6,21):
    print('Weird')
    elif i%2==0 and i>20:
    print('Not Weird')

    ReplyDelete
  33. please soeone rectify my code
    n=int(input())
    if n%2!==0 or (620:
    print("Not Weird")

    ReplyDelete
  34. n = int(input())
    if n % 4 == 1:
    print("Weird")
    elif n % 4 == 0 and 2 <= n <= 6:
    print("Not Weird")
    elif n % 4 == 0 and 8 <= n <= 30:
    print("Weird")
    else:
    print("Not Weird")

    ReplyDelete
  35. https://capbrain.blogspot.com/p/web-stories.html

    ReplyDelete

Powered by Blogger.