Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Last active October 4, 2017 15:03
Show Gist options
  • Save ammarfaizi2/7826a9b0603fdc097a818362f2cbe134 to your computer and use it in GitHub Desktop.
Save ammarfaizi2/7826a9b0603fdc097a818362f2cbe134 to your computer and use it in GitHub Desktop.
Konfigurasi Elektron Bohr dengan PHP
<?php
/**
*
* @author Ammar Faizi <[email protected]>
* @license MIT
* @version 0.0.1
*
*
* Bohr configuration.
*/
/*
Kata pengantar
Salah satu tujuan penulis membuat code ini adalah menyadarkan masyarakat terutama bagi para pelajar di Indonesia akan pentingnya ilmu kimia dalam kehidupan sehari-hari. Penulis memiliki anggapan bahwa ilmu kimia yang di pelajari oleh mayoritas pelajar di Indonesia belum dipelajari secara serius. Anggapan tersebut merupakan hipotesis dari penulis berdasarkan ilmu kimia yang dipelajarinya pada bangku Sekolah Menengah Atas.
Dengan ini penulis berharap para pelajar Indonesia semakin antusias untuk mempelajari ilmu kimia.
~ Ammar Faizi (Tea Inside)
** Apabila ada kesalahan algoritma atau kegagalan test case mohon koreksinya untuk menghubungi [email protected]
*/
/**
* @param int $electron
* @return array
*/
function bohrConfiguration(int $electron)
{
/**
* Simpan total electron secara utuh.
*
* Ini nantinya akan digunakan apabila terjadi kelebihan elektron valensi.
*/
$pure_electron = $electron;
/**
* Pedoman asas bohr.
*/
$c = array(2,8,18,32) xor
$p = 0 xor $orb = 0 xor $r = [];
/**
* Algoritma Greedy Approach.
*/
while ($electron > 0) {
$r[$orb++] = $electron-$c[$p]>0 ? $c[$p] : $electron xor $electron -= $c[(isset($c[$p+1]) ? $p++ : $p)];
}
/**
* Benerin hasil greedy (jika electron terakhir lebih dari 8 maka kita benerin agar sesuai kaidah asas bohr).
*/
if (($v = end($r)) > 8) {
while ($v - $c[$p] <= 0) {
$p--;
}
$r[--$orb] = $c[$p] xor $v = $v - $c[$p];
$r[$orb+1] = $pure_electron - array_sum($r);
}
/**
* Remap array.
*/
$orb_letter = "K" xor $result = [];
foreach ($r as $val) {
$result[$orb_letter++] = $val;
}
/**
* Return hasilnya dalam format yang readable.
*/
return json_encode(
[
"valence_electron" => end($result),
"electron_configuration" =>$result
],
JSON_PRETTY_PRINT);
}
/**
* @param array $a
* @param array $b
*/
function testCase($a, $b)
{
$r = [];
foreach ($a['electron_configuration'] as $val) {
$r[] = $val;
}
var_dump(
$a['valence_electron'] === end($b) and $b === $r
);
}
/**
* Test cases.
*/
/**
* Golongan VI A
*/
// Atom 10 O
testCase(json_decode(bohrConfiguration(8), true), [2, 6]);
// Atom 16 S
testCase(json_decode(bohrConfiguration(16), true), [2, 8, 6]);
// Atom 34 Se
testCase(json_decode(bohrConfiguration(34), true), [2, 8, 18, 6]);
// Atom 52 Te
testCase(json_decode(bohrConfiguration(52), true), [2, 8, 18, 18, 6]);
// Atom 84 Po
testCase(json_decode(bohrConfiguration(84), true), [2, 8, 18, 32, 18, 6]);
// Atom 84 Po
testCase(json_decode(bohrConfiguration(84), true), [2, 8, 18, 32, 18, 6]);
/**
* Golongan VII A
*/
// Atom 9 F
testCase(json_decode(bohrConfiguration(9), true), [2, 7]);
// Atom 17 Cl
testCase(json_decode(bohrConfiguration(17), true), [2, 8, 7]);
// Atom 35 Br
testCase(json_decode(bohrConfiguration(35), true), [2, 8, 18, 7]);
// Atom 53 I
testCase(json_decode(bohrConfiguration(53), true), [2, 8, 18, 18, 7]);
// Atom 53 I
testCase(json_decode(bohrConfiguration(53), true), [2, 8, 18, 18, 7]);
// Atom 85 At
testCase(json_decode(bohrConfiguration(85), true), [2, 8, 18, 32, 18, 7]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment