Skip to main content

Posts

Showing posts with the label array

MSB bits

  #!/bin/python3 import math import os import random import re import sys # # Complete the 'getOneBits' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts INTEGER n as parameter. # def getOneBits ( n ):     # Write your code here     if n == 0 :         return [ 0 ]             binary = bin ( n )[ 2 :]     d = []     pos = 1     temp = n     for idx , bit in enumerate ( binary , 1 ) :         if bit == '1' :                         d . append ( idx )             return [ len ( d )] + d     if __name__ == '__main__' :     fptr = open ( os . environ [ 'OUTPUT_PATH' ], 'w' )     n = int ( input () . strip ())     result = getOneBits ( n )     fptr ...

int to Roman Number

  #!/bin/python3 import math import os import random import re import sys # # Complete the 'romanizer' function below. # # The function is expected to return a STRING_ARRAY. # The function accepts INTEGER_ARRAY numbers as parameter. # def romanizer ( numbers ):     # Write your code here     def intToRoman ( num ):         val = [             1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1         ]         syms = [             "M" , "CM" , "D" , "CD" ,             "C" , "XC" , "L" , "XL" ,             "X" , "IX" , "V" , "IV" ,             "I"         ]         roman = ""         i = 0         while num > 0 :           ...

Roman Number to Integer

This website describe common assumption and failure during gfg coding. python Failed class Solution: def romanToDecimal(self, s): # code here val={ '':1, 'X':10, 'L':50, 'M':1000, 'C':100, 'V':5 } sum=0 s=s[::-1] count=0 for i in s: if count==1: count=0 sum-=val[i] else: sum+=val[i] count+=1 return sum Reason Your current implementation of `romanToDecimal()` has the right spirit, but the logic for subtractive notation (like `IV`, `IX`, `XL`, etc.) is off. You're using a `count` flag, which doesn't reliably detect when subtraction should occur. --- ⚠️ Issues: 1. Incorrect subtraction logic : Roman numerals subtract only when a smaller value precedes a larger...