fork download
  1. # -------- Muhammad Yalqa R.A. - Suitmedia Backend Test --------
  2.  
  3. # -------- DEKLARASI FUNCTION --------
  4. def hitungNomorBit(angka, nomorBit):
  5. biner = []
  6. temp = angka
  7. while temp > 0:
  8. biner.insert(0, temp % 2)
  9. temp = temp // 2
  10.  
  11. hasil = []
  12. grup = []
  13. for bit in biner:
  14. if bit == 1:
  15. grup.append(bit)
  16. else:
  17. if grup:
  18. hasil.append(grup)
  19. grup = []
  20. if grup:
  21. hasil.append(grup)
  22.  
  23. hasil = hasil[::-1]
  24.  
  25. if nomorBit < len(hasil):
  26. grup_terpilih = hasil[nomorBit]
  27. return int("".join(str(b) for b in grup_terpilih), 2)
  28. else:
  29. return None
  30.  
  31.  
  32. # -------- PROGRAM UTAMA --------
  33. if __name__ == "__main__":
  34. print("Contoh otomatis hitungNomorBit(13, x):")
  35. print("hitungNomorBit(13, 0) =", hitungNomorBit(13, 0)) # Output: 1
  36. print("hitungNomorBit(13, 1) =", hitungNomorBit(13, 1)) # Output: 3
  37. print("hitungNomorBit(13, 2) =", hitungNomorBit(13, 2)) # Output: None
  38. print("")
  39.  
  40. try:
  41. angka = int(input())
  42. nomorBit = int(input())
  43.  
  44. hasil = hitungNomorBit(angka, nomorBit)
  45. if hasil is not None:
  46. print(f"Hasil hitungNomorBit({angka}, {nomorBit}) adalah: {hasil}")
  47. else:
  48. print(f"Hasil hitungNomorBit({angka}, {nomorBit}) adalah: null")
  49. except ValueError:
  50. print("Input tidak valid. Harap masukkan angka bulat.")
Success #stdin #stdout 0.11s 13996KB
stdin
13
1
stdout
Contoh otomatis hitungNomorBit(13, x):
hitungNomorBit(13, 0) = 1
hitungNomorBit(13, 1) = 3
hitungNomorBit(13, 2) = None

Hasil hitungNomorBit(13, 1) adalah: 3