Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save glenkusuma/74ce7448124fc30687a46febdc98f206 to your computer and use it in GitHub Desktop.
Save glenkusuma/74ce7448124fc30687a46febdc98f206 to your computer and use it in GitHub Desktop.
Permutasi dan Menghitung Kombinasi

Permutasi & Minghitung Kombinasi

Creator

Soal

Tentukan jumlah susunan yang akan diperoleh, jika hendak menghabiskan uang sejumlah $100 untuk tepat membeli 100 ayam, kriteria jenis ayam dan harganya ditentukan berikut:

  • harga 1 ayam pejantan = $6
  • harga 1 ayam betina = $3
  • harga 10 anak ayam = $1

Solved with Python

import itertools

# Create list
jantan = [i for i in range(0,100,6)]
betina = [i for i in range(0,100,3)]
anak = [i for i in range(0,100,1)]

# create datastore with list 
data = [jantan, betina, anak]
# Create permutasi from data
permutasi = list(itertools.product(*data))

print('Banyak kemungkinan', len(permutasi))

# looping permutasi jadi list
for dollar in permutasi:   
  # total ayam
  ayam = (int(dollar[0]/6), int(dollar[1]/3), dollar[2]*10)
  # total beli
  beli = (int(dollar[0]/6), int(dollar[1]/3), dollar[2])

  # kondisi kombinasi 100 dollar dan 100 ayam
  if(sum(dollar) == 100) and sum(ayam) == 100:
    kombinasi_beli = f"Beli   : {beli}"
    kombinasi_dollar = f"Dollar : {dollar} = {sum(dollar)}"
    kombinasi_ayam = f"Ayam   : {ayam} = {sum(ayam)}"

    print('Ket: (Jantan, Betina, Anak Ayam)\n')
    print(kombinasi_beli)
    print(kombinasi_dollar)
    print(kombinasi_ayam)

Outputs:

Banyak kemungkinan 57800
Ket: (Jantan, Betina, Anak Ayam)

Beli   : (1, 29, 7)
Dollar : (6, 87, 7) = 100
Ayam   : (1, 29, 70) = 100

Created With Love,

April 2022

Bandung, Indonesia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment