I was wondering what the difference in grammar would look like if I programmed with the same subject, so I created a simple program so that I could compare it. The description method is different for each language, but the common control statement for for and if is used, and the replacement process is the same. I realized once again that I thought language was a tool for system development.
PHP
$dt = array(7,5,9,6,2,4,1,3,8);
for($i = 0; count($dt) > $i; $i++){
foreach($dt as $k=>$v){
$set_k = $k + 1;
if(isset($dt[$set_k]) && $v > $dt[$set_k]){
$tmp = $dt[$set_k];
$dt[$set_k] = $dt[$k];
$dt[$k] = $tmp;
}
}
}
Java
int dt[] = {7,5,9,6,2,4,1,3,8};
for(int i = 0;dt.length - 1 > i;i++){
for(int j = 0;dt.length - 1 > j;j++){
int k = j + 1;
if(dt[j] > dt[k]){
int tmp = dt[k];
dt[k] = dt[j];
dt[j] = tmp;
}
}
}
JavaScript
let dt = [7,5,9,6,2,4,1,3,8];
for(let i = 0;dt.length - 1 > i;i++){
for(let j = 0;dt.length - 1 > j;j++){
let k = j + 1;
if(dt[j] > dt[k]){
let tmp = dt[k];
dt[k] = dt[j];
dt[j] = tmp;
}
}
}
Python
dt = [7,5,9,6,2,4,1,3,8]
for i in dt:
for j in dt:
k = dt.index(j) + 1
if len(dt) > k and j > dt[k]:
tmp = dt[k]
dt[k] = j
dt[dt.index(j)] = tmp
Ruby
dt = [7,5,9,6,2,4,1,3,8]
for a in dt do
dt.map.with_index{|v,i|
k = i + 1
if !dt[k].nil? && v > dt[k]
tmp = dt[k]
dt[k] = v
dt[i] = tmp
end
}
end
Recommended Posts