Using unique random numbers for a switch statement - php

I need a function or an array that gives me random numbers from 1 - 47, so I can use it for my code below.
public function output($question_id, $solution) {
$yes = ($GLOBALS['TSFE']->sys_language_uid == 0) ? 'Ja' : 'Oui';
$no = ($GLOBALS['TSFE']->sys_language_uid == 0) ? 'Nein' : 'No';
switch ($question_id) {
case 1:
$arg = array(
'main_content' => $this->build_html(1, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
);
break;
case 2:
$arg = array(
'main_content' => $this->build_html(2, '02_0342_05_14_Psori_Arthropathie.jpg', $yes, $no, $solution),
);
break;
case 3:
$arg = array(
'main_content' => $this->build_html(3, '03_0255_05_14_Psori_Arthropathie.jpg', $yes, $no, $solution),
);
break;
}
}
Example
This
case 1:
$arg = array(
'main_content' => $this->build_html(1, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
);
should look somehow like this:
case $random_id:
$arg = array(
'main_content' => $this->build_html($random_id, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
);
So that means, each case and each first parameter of the function build_html should get a unique random id.
Of course I could use rand() but then it is quite possible that I get duplicated values.
Any help would be appreciated!

Create a class like follows:
class SamplerWithoutReplacement {
private $pool;
public __construct($min,$max) {
$this->pool = range($min,$max);
}
public function next() {
if (!empty($this->pool)) {
$nIndex = array_rand($this->pool);
$value = $this->pool[$nIndex];
unset($this->pool[$nIndex]);
return $value;
}
return null; //Or throw exception, depends on your handling preference
}
}
Use it as:
$sampler = new SamplerWithoutReplacement(1,47);
//do things
$value = $sampler->next();
The $sampler will draw random samples between 1-47 without replacing them in the pool and therefore they'll be unique.

Not tested but I think it should work:
$numbers = [];
function genenerate_nr_not_in_list($list, $min = 1, $max = 47) {
$number = rand($min, $max);
while (false !== array_search($number, $list)) {
$number = rand($min, $max);
}
return $number;
}
for ($i = 0; $i < 47; $i++) {
$numbers[] = genenerate_nr_not_in_list($numbers);
}
With this solution you can generate numbers in each range ($min and $max parameters). But it should be at least as big as the number of values you need ($max).

Related

How to take a single record from an array

I have a function that counts the number of points for each letter. I want her to count the points for each word. See this is my code:
function getValue() {
$letter = $this->getName(); // String from FORM
// Switch looks at a letter and assigns the value points for that letter
switch(true){
case($letter == 'a'||$letter == 'e'||$letter == 'i'||$letter == 'o'||$letter == 'u'||$letter == 'l'||$letter == 'n'||$letter == 's'||$letter == 't'||$letter == 'r'):
return 1;
case($letter == 'd'||$letter == 'g'):
return 2;
case($letter == 'b'||$letter == 'c'||$letter == 'm'||$letter == 'p'):
return 3;
case($letter == 'f'||$letter == 'h'||$letter == 'v'||$letter == 'w'||$letter == 'y'):
return 4;
case($letter == 'k'):
return 5;
case($letter == 'j'||$letter == 'x'):
return 8;
case($letter == 'q'||$letter == 'z'):
return 10;
default:
return 0;
}
}
function makeWordsPoint() {
$total_word_points = 0;
$words = $this->word_for_letters;
foreach ($words as $word) {
$total_word_points = $word->getValue();
}
echo $word . "=" . $total_word_points
}
How I can do it? Thanks for help
EDIT:
Okey, look now. There is my two classes Word and Letter
<?php
class Word
{
private $word;
private $words_with_points = array();
function __construct($user_letters)
{
$this->word = $user_letters;
// creates array of object word for letters
$this->word_for_letters = $this->makeWordForLetters();
// creates array of letter objects for the word
$this->words_with_points = $this->makeWordsWithPoints();
}
function makeWordForLetters()
{
$word_objects = array();
$word = $this->getWord();
$file = file_get_contents( __DIR__."/../src/dictionary.txt");
$items = explode("\n", $file);
$letters = str_split($word);
foreach ($items as $item) {
$list = $letters;
// remove the original word (once)
$thisItem = preg_replace("/$word/", '', $item, 1);
for ($i = 0; $i < strlen($thisItem); $i++) {
$index = array_search($thisItem[$i], $list);
if ($index === false) {
continue 2; // letter not available
}
unset($list[$index]); // remove the letter from the list
}
array_push($word_objects, $item);
}
return $word_objects; // passed!
}
function makeWordsWithPoints()
{
$word = $this->makeWordForLetters();
$letter_objects = array();
foreach ($word as $character) {
array_push($letter_objects, new Letter($character));
}
return $letter_objects;
}
function getWord()
{
return $this->word;
}
function getWordForLetters()
{
return $this->word_for_letters;
}
function getWordsWithPoints()
{
return $this->words_with_points;
}
}
?>
<?php
class Letter
{
private $name;
private $value;
function __construct($letter)
{
$letter = strtolower($letter);
$this->name = $letter;
$this->value = $this->setValue();
}
function getName()
{
return $this->name;
}
function getValue()
{
return $this->value;
}
function setValue()
{
$letter = $this->getName();
switch(true){
case($letter == 'a'||$letter == 'e'||$letter == 'i'||$letter == 'o'||$letter == 'u'||$letter == 'l'||$letter == 'n'||$letter == 's'||$letter == 't'||$letter == 'r'):
return 1;
case($letter == 'd'||$letter == 'g'):
return 2;
case($letter == 'b'||$letter == 'c'||$letter == 'm'||$letter == 'p'):
return 3;
case($letter == 'f'||$letter == 'h'||$letter == 'v'||$letter == 'w'||$letter == 'y'):
return 4;
case($letter == 'k'):
return 5;
case($letter == 'j'||$letter == 'x'):
return 8;
case($letter == 'q'||$letter == 'z'):
return 10;
default:
return 0;
}
}
}
?>
And now when I write in now letters like this: loso function makeWordForLetters() search in my array correctly words for this letters and I display this words with points by makeWordsWithPoint like this:
l - 1
lo - 0
loo - 0
loos - 0
los - 0
oslo - 0
s - 1
solo - 0
But as you can see the score is incorrect because it displays the result for a single letter and not for a word.
How can I solve this problem?
take it as string, then use preg_split function, count new array length.eg:
$string="php教程#php入门:教程#字符串:多分隔符#字符串:拆分#数组";
$arr = preg_split("/(#|:)/",$string);
print_r($arr);
Try this code instead. I think it's cleaner.
<?php
// set the score of each char into array
const SCORES = [
// 1
'a'=> 1,
'e' => 1,
'i' => 1,
'o' => 1,
'u' => 1,
'l' => 1,
'n' => 1,
's' => 1,
't' => 1,
'r' => 1,
// 2
'd'=> 2,
'g'=> 2,
// 3
'b'=> 3,
'c'=> 3,
'm'=> 3,
'p'=> 3,
// 4
'f'=> 4,
'h'=> 4,
'v'=> 4,
'w'=> 4,
'y'=> 4,
// 5
'k'=> 5,
// 8
'j'=> 8,
'x'=> 8,
// 10
'q'=> 10,
'z'=> 10,
];
$word = 'abcdef'; // get the string from the request here
# print_r($word);
$chars = str_split($word); // split string into array of chars
# print_r($chars);
$scores = array_map(function($char) { // create a scores array that convert char into value
return SCORES[strtolower($char)] ?? 0; // get the score of each char and set to the array, if not exist set to 0
}, $chars);
# print_r($scores);
$totalScore = array_sum($scores); // get the sum of the scores
echo $word . "=" . $totalScore;
Let me know if you have any question.

How to reset variable a,b,c,and score after loop?

i want to count my score from option a/b/c that inputted from a form. for each input given, value from a =1,b=2,c=3 ,after count i want to save the result in score variable
in my controller
public function score($id) {
$user_login = Auth::user()->id;
$answers = Answer::select('user_answer')->where('jenis_quiz_id','=',$id)->where('user_id','=',$user_login)->get();
static $a = 0;
static $b = 0;
static $c = 0;
static $score = 0;
if($answers->count()) {
foreach ($answers as $answer) {
if ($answer->user_answer == '1') {
$a++;
} else if($answer->user_answer == '2') {
$b++;
} elseif($answer->user_answer == '3') {
$c++;
}
}
}
$score = $a+$b+$c;
$returnScore = $score;
$a = null;
$b = null;
$c = null;
$score = null;
return $returnScore;
}
public function getShowResultOfQuiz($id) {
$categoryquiz = JenisQuiz::findOrFail($id);
$user = Auth::user()->id;
$score= $this->score($id);
$kelas = Auth::user()->kelas;
$instansi = Auth::user()->instansi;
History::create([
'user_id'=>$user,
'jenis_quiz_id'=>$id,
'score'=> $score,
'kelas' => $kelas,
'instansi' => $instansi
]);
// $time_taken = date("H:i:s", strtotime(Answer::whereJenisQuizId($id)->orderBy('id', 'desc')->first()->time_taken));
switch ($id) {
case '1':
return view('quiz1',compact('score','categoryquiz'));
case '2':
return view('quiz2',compact('score','categoryquiz'));
case '3':
return view('quiz3',compact('score','categoryquiz'));
}
}
i want to reset a,b,c,and score variables every input submitted,and the problem is score always added from previous submitted answer instead of reset it before calculating the score again, help me , thanks

How get a (fixed) value based on number range

I have a number, e.g. $humidity
I need to check value of this number and get a fixed value if number is in a predetermined range.
E.g.
if ($humidity<30) {
return 'dry';
}
if ($humidity>=30 && $humidity <=40) {
return 'wet'
}
if ($humidty>40 && $humidity<70) {
return 'confortable'
}
And so on.
Is there another possibility to don't use 4/5 different if ?
As long as you process the values in order, you don't need both the upper and lower values of each range. Then you can utilize short-circuiting and just put everything in a loop:
function nameOf($humidity)
{
$list = [
30 => 'dry',
40 => 'wet',
70 => 'comfortable',
];
foreach ($list as $value => $name) {
if ($humidity < $value) {
return $name;
}
}
return 'default';
}
I think switch is great for this usage:
$result = null;
switch(true) {
case $humidity < 30:
$result = 'dry';
break;
case $humidity >= 30 && $humidity < 40:
$result = 'wet';
break;
case $humidty > 40 && $humidity < 70:
$result = 'comfortable';
break;
}
return $result;
you can create an range array for your temps and then array_walk that to find the right range.
$h=40;
$harr = [ 0 => 'dry', 1 => 'wet', 2 => 'confy'];
$range = array_flip(range(0, 100, 30));
array_walk($range, 'findHumidity', $range);
var_dump($harr[$result]);
function findHumidity($value, $key, $r){
global $h; //if you using a class and properties you can ignore these two lines
global $result;
$normalRange = array_flip($r);
if($h> $key && $normalRange[$value+1]!=null && $h<= $normalRange[$value+1]){
$result = $value;
}
}
Working example: https://3v4l.org/fWLDu (a simplified version: https://3v4l.org/ROjWk)
or you can define the range array manually like here: https://3v4l.org/B0oTU

variable dynamic span overlap

$lines = file("res_example.txt");
$resArr = array();
foreach ($lines as $line_num => $line) {
$columns = explode("\t", $line);
$raws = $columns['1'];
$hits = $columns['2'];
$names = $columns['0'];
$models = $columns['3'];
$colors = $columns['4'];
$allModels[$models] = 1;
$resArr[] = array(
name => $names,
score => $raws,
hit => $hits,
model => $models,
color => $colors
);
}
$seqArr = array('A', 'T', 'C', 'G');
$randseq = array();
for ($i = 0; $i < 1000; $i++) {
$randseq[] = $seqArr[array_rand($seqArr)];
}
$res = "";
echo "<div id=\"coltext\" style=\"font-family:monospace;\">";
foreach ($allModels as $modName => $value) {
echo "<input ModelName=$modName type=\"checkbox\"
checked==\"TRUE\" onclick=\"toggle.apply(this)\" />$modName";
}
echo "<hr />";
$score = rawtransform($raw);
foreach ($randseq as $index => $nuc) {
$index = $index + 1;
foreach ($resArr as $hitArr) {
$hit = $hitArr['hit'];
$raw = $hitArr['score'];
$model = $hitArr['model'];
$colval = $hitArr['color'];
$score = rawtransform($raw);
$color = getcolor($score, $colval);
if (($hit+3) == $index) {
echo "</span>";
}
if ($hit == $index) {
echo "<span class=$model Title=\"position:$index,score:$raw\"
style=\"background:$color;\" color=\"$color\">";
//problem when theres overlap !?
}
}
echo $nuc;
if (($index%50)==0){
echo"<br />";
}
}
echo "</div>";
function rawtransform($raw) {
return (int)($raw/50)-9;
}
function getcolor($score,$ArrayModelval)
{
switch ($score){
// working. test each color.
case 1: /*500-550(Raw Score)*/
$col=$ArrayModelval;
return"hsl( $col,100%,90%)";
break;
case 2: //550-600
$col=$ArrayModelval;
return "hsl( $col,100%,85%)";
break;
case 3: //600-650
$col=$ArrayModelval;
return "hsl( $col,100%,85%)";
break;
case 4: //650-700
$col=$ArrayModelval;
return"hsl( $col,100%,80%)";
break;
case 5: //700-750
$col=$ArrayModelval;
return"hsl( $col,100%,70%)";
break;
case 6: //750-800
$col=$ArrayModelval;
return "hsl( $col,100%,60%)";
break;
case 7: //800-850
$col=$ArrayModelval;
return "hsl( $col,100%,50%)";
break;
case 8: //850-900;
$col=$ArrayModelval;
return "hsl( $col,100%,50%)";
break;
case 9: //900-950
$col=$ArrayModelval;
return "hsl( $col,100%,40%)";
break;
case 10: //950-1000
$col=$ArrayModelval;
return "hsl($col,100%,40%)";
break;
}
}
For the most part does what I want: I want to color parts of the random seqeunce where there is a $hit - defined on external file. My only problem is when there is any overlap, i.e. if two hits are within 3 bases of each other the span is elongated and colored as if its one span.
The external file has a position to start a span which have variable colors depending on a score given in the external file. Basically if I have 3 results, 2 of which have almost the same hit (+-1) and the other a different hit, I would only see two sections colored, can anyone see what my problem is? Sorry I know I probably worded this horribly but its hard to explain. Thanks.
>chr1:2198584545754_genome_1000+ 500 1000 Model1 0
>chr2:2198581212154_genome_1000+ 510 992 Model2 180
>chr3:2115151215754_genome_1000+ 520 990 Model3 330
>chr4:2198584545754_genome_1000+ 530 980 Model3 330
>chr5:1218455145754_genome_1000+ 540 970 Model2 180
>chr6:1231354645454_genome_1000+ 550 960 Model1 0
>chr7:1231213211134_genome_1000+ 600 950 Model3 330
>chr7:1231213211134_genome_1000+ 650 940 Model3 330
javascript:
function toggle() {
var div= document.getElementById('coltext');
var modName=this.getAttribute('ModelName');
var spans=div.getElementsByTagName('span');
var spans_l=spans.length;
while (spans_l--){
span=spans[spans_l];
if(span.getAttribute('class')==modName && this.checked==true){
var color= span.getAttribute('color');
span.style.background=color;
}
if(span.getAttribute('class')==modName && this.checked==false){
span.style.background="white";
}
}
}
Try this on for size. It works by using a FIFO stack $currentHits to handle the hit boundaries. I have also added a few helper functions to deal with color generation - if you alter your getcolor() function to return an array instead of a CSS string one of them could be dropped, making it more efficient.
I have been unable to test this because I don't have the source code for your getcolor() or rawtransform() functions - if you add these to the question I am certain further improvements can be made and I can test the code properly. Also, it would be good to see what your CSS Model1, Model2 etc classes look like.
Edit: now includes getcolor()/rawtransform() functions and (at least partially) tested
<?php
function rawtransform ($raw) {
return (int) ($raw / 50) - 9;
}
function getcolor ($score, $h) {
switch ($score) {
// working. test each color.
case 1: /*500-550(Raw Score)*/
$l = 90;
break;
case 2: case 3: //550-650
$l = 85;
break;
case 4: //650-700
$l = 80;
break;
case 5: //700-750
$l = 70;
break;
case 6: //750-800
$l = 60;
break;
case 7: case 8: //800-900;
$l = 50;
break;
case 9: case 10: default: //900-1000 / out of range
$l = 40;
break;
}
return array(
'h' => $h,
's' => 100,
'l' => $l
);
}
function hsl_average_color () {
// Takes an unlimited number of arguments, calculates the average HSL value and returns a CSS string
$args = func_get_args();
$h = $s = $l = array();
foreach ($args as $arg) {
$h[] = $arg['h'];
$s[] = $arg['s'];
$l[] = $arg['l'];
}
return sprintf('hsl(%d, %d%%, %d%%)', (int) round(array_sum($h) / count($h)), (int) round(array_sum($s) / count($s)), round(array_sum($l) / count($l)));
}
$fileName = 'res_example.txt';
// Open the file
if (!$fp = fopen($fileName, 'r')) {
// Handle file read errors here
die("Unable to open file $fileName");
}
// Loop the file data and build an associative array
$resArr = array();
while (($line = fgetcsv($fp, 0, "\t")) !== FALSE) {
// You didn't declare $allModels as an empty array before the loop
// Should you have?
$allModels[$line[3]] = 1;
// Note that I have dropped the hit key and instead keyed the outer
// array by this value. I have added an end key to track the end of
// a hit
$resArr[$line[2]] = array(
'name' => $line[0],
'score' => $line[1],
'end' => $line[2] + 4,
'model' => $line[3],
'color' => getcolor(rawtransform($line[1]), $line[4])
);
}
// Close the file
fclose($fp);
// Generate a random sequence
$seqArr = array('A', 'T', 'C', 'G');
$randseq = array();
for ($i = 0; $i < 1000; $i++) {
$randseq[] = $seqArr[array_rand($seqArr)];
}
// $res appears to do nothing in you code
// $res = "";
// Open the <div>
echo '<div id="coltext" style="font-family:monospace;background-color:#000000;color:#FFFFFF;">'."\n";
// Iterate over $allModels and echo checkboxes
foreach ($allModels as $modName => $value) {
// ModelName is a non-standard HTML attribute, are you sure you meant to do this?
echo '<input ModelName="'.$modName.'" type="checkbox" checked="checked" onclick="toggle.apply(this);" />'.$modName."\n";
}
echo "<hr />\n";
// This line does nothing useful here
// $score = rawtransform($raw);
// An array to track the current hits
$currentHits = array();
foreach ($randseq as $index => $nuc) {
// Increment $index
$index++;
// Track whether we are in a hit/reached a boundary
$boundary = FALSE;
$inHit = (bool) count($currentHits);
// Check whether we are at the end of the lowest hit in the stack
if ($inHit && $index == $currentHits[0]['end']) {
$boundary = TRUE;
array_shift($currentHits);
}
// Check whether we are at the start of a new hit
if (isset($resArr[$index])) {
$boundary = TRUE;
$currentHits[] = $resArr[$index];
}
// If we reached a boundary
if ($boundary) {
// Close a hit
if ($inHit) {
echo "</span>";
}
// Open a hit
if (count($currentHits)) {
// Get the current color value
$colors = array();
foreach ($currentHits as $hit) $colors[] = $hit['color'];
$color = call_user_func_array('hsl_average_color', $colors);
// Start a new span
echo '<span class="'.$currentHits[0]['model'].'" title="position:'.$index.',score:'.$currentHits[0]['score'].'" style="color: '.$color.';">';
}
}
// Print the character
echo $nuc;
// Split into 50 character chunks
if (!($index % 50)){
echo"<br />\n";
}
}
// Close the last span if one is still open
if (count($currentHits)) {
echo "</span>";
}
// Close the <div>
echo "</div>\n";

php switch case statement to handle ranges

I'm parsing some text and calculating the weight based on some rules. All the characters have the same weight. This would make the switch statement really long can I use ranges in the case statement.
I saw one of the answers advocating associative arrays.
$weights = array(
[a-z][A-Z] => 10,
[0-9] => 100,
['+','-','/','*'] => 250
);
//there are more rules which have been left out for the sake of clarity and brevity
$total_weight = 0;
foreach ($text as $character)
{
$total_weight += $weight[$character];
}
echo $weight;
What is the best way to achieve something like this?
Is there something similar to the bash case statement in php?
Surely writing down each individual character in either the associative array or the switch statement can't be the most elegant solution or is it the only alternative?
Well, you can have ranges in switch statement like:
//just an example, though
$t = "2000";
switch (true) {
case ($t < "1000"):
alert("t is less than 1000");
break
case ($t < "1801"):
alert("t is less than 1801");
break
default:
alert("t is greater than 1800")
}
//OR
switch(true) {
case in_array($t, range(0,20)): //the range from range of 0-20
echo "1";
break;
case in_array($t, range(21,40)): //range of 21-40
echo "2";
break;
}
$str = 'This is a test 123 + 3';
$patterns = array (
'/[a-zA-Z]/' => 10,
'/[0-9]/' => 100,
'/[\+\-\/\*]/' => 250
);
$weight_total = 0;
foreach ($patterns as $pattern => $weight)
{
$weight_total += $weight * preg_match_all ($pattern, $str, $match);;
}
echo $weight_total;
*UPDATE: with default value *
foreach ($patterns as $pattern => $weight)
{
$match_found = preg_match_all ($pattern, $str, $match);
if ($match_found)
{
$weight_total += $weight * $match_found;
}
else
{
$weight_total += 5; // weight by default
}
}
You can specify the character range using regular expression. This saves from writing a really long switch case list. For example,
function find_weight($ch, $arr) {
foreach ($arr as $pat => $weight) {
if (preg_match($pat, $ch)) {
return $weight;
}
}
return 0;
}
$weights = array(
'/[a-zA-Z]/' => 10,
'/[0-9]/' => 100,
'/[+\\-\\/*]/' => 250
);
//there are more rules which have been left out for the sake of clarity and brevity
$total_weight = 0;
$text = 'a1-';
foreach (str_split($text) as $character)
{
$total_weight += find_weight($character, $weights);
}
echo $total_weight; //360
Much different ways to do this.
$var = 0;
$range_const = range(10,20);
switch ($var) {
case 1: $do = 5; break; # 1
case 2: $do = 10; break; # 2
case 3:
case 4:
case 5: $do = 15; break; # 3, 4, 5
default:
if ($var > 5 && $var < 10) { # High performance (6..9)
$do = 20;
} else if (in_array($var, $range_const, true)) { # Looks clear (10..20)
$do = 25;
} else { # NOT in range 1..20
$do = -1;
}
}
print($do);
There no direct range X..Y compares because $var checks to true in each step, but this allows do some nice cheating like this...
$in = create_function('$a,$l,$h', 'return $a>=$l && $a<=$h;');
$var = 4;
switch (true) {
case ($var === 1): echo 1; break;
case ($var === 2): echo 2; break;
case $in($var, 3, 5): echo "3..5"; break;
case $in($var, 6, 10): echo "6..10"; break;
default: echo "else";
}
If you have a more complex conditions, you can wrap them inside a function. Here's an oversimplified example:
$chartID = 20;
$somethingElse = true;
switch (switchRanges($chartID, $somethingElse)) {
case "do this":
echo "This is done";
break;
case "do that":
echo "that is done";
break;
default:
echo "do something different";
}
function switchRanges($chartID, $somethingElse = false)
{
if (in_array($chartID, [20, 30]) && $somethingElse === true) {
return "do this";
}
if (in_array($chartID, [20, 50]) && $somethingElse === false) {
return "do that";
}
}
I think I would do it in a simple way.
switch($t = 100){
case ($t > 99 && $t < 101):
doSomething();
break;
}

Categories