Hi there surajkay19,
from @Paul_Wilkins’s excellent link,
take a look at this HTML example…
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>untitled document</title>
<link rel="stylesheet" href="screen.css" media="screen">
<style media="screen">
body {
background-color: #f0f0f0;
font: 1em/150% verdana, arial, helvetica, sans-serif;
}
#combinations {
max-width: 78.125em;max-width: 79em;
padding: 1.25em;
margin: auto;
border: 0.078em solid #999;
box-sizing:border-box;
background-color: #fff;
font-family:'courier new' , monospace;
font-size: 0.8em;
}
</style>
</head>
<body>
<div id="combinations"></div>
<script>
((d) => {
'use strict';
// COMBINATIONS -----------------------------------------------------------
// comb :: Int -> Int -> [[Int]]
const comb = (m, n) => combinations(m, enumFromTo(1, n ));
// combinations :: Int -> [a] -> [[a]]
const combinations = (k, xs) =>
sort(filter(xs => k === xs.length, subsequences(xs)));
// GENERIC FUNCTIONS -----------------------------------------------------
// cons :: a -> [a] -> [a]
const cons = (x, xs) => [x].concat(xs);
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
// foldr (a -> b -> b) -> b -> [a] -> b
const foldr = (f, a, xs) => xs.reduceRight(f, a);
// isNull :: [a] -> Bool
const isNull = xs => (xs instanceof Array) ? xs.length < 1 : undefined;
// show :: a -> String
const show = x => JSON.stringify(x) //, null, 2);
// sort :: Ord a => [a] -> [a]
const sort = xs => xs.sort();
// stringChars :: String -> [Char]
const stringChars = s => s.split('');
// subsequences :: [a] -> [[a]]
const subsequences = xs => {
// nonEmptySubsequences :: [a] -> [[a]]
const nonEmptySubsequences = xxs => {
if (isNull(xxs)) return [];
const [x, xs] = uncons(xxs);
const f = (r, ys) => cons(ys, cons(cons(x, ys), r));
return cons([x], foldr(f, [], nonEmptySubsequences(xs)));
};
return nonEmptySubsequences(
(typeof xs === 'string' ? stringChars(xs) : xs)
);
};
// uncons :: [a] -> Maybe (a, [a])
const uncons = xs => xs.length ? [xs[0], xs.slice(1)] : undefined;
// TEST -------------------------------------------------------------------
d.getElementById('combinations').textContent= show(
comb(5, 15)
)
})(document);
</script>
</body>
</html>
Good luck with learning the javascript, it should
keep totally occupied for the next year or so.
coothead
2 Likes
Language DOES matter it should be Server Side
Source Code for PHP Groups:
# $item = 99999;
# $group = 15;
$array = range(1, $items);
shuffle($array);
$row = '';
$id = 0;
for( $offset = 0; $offset < count($array); $offset += $group):
$tmp = $array;
$tmp = array_slice($tmp, $offset, $group);
$row .= formatGroup($tmp, ++$id);
endfor;
**Source code for web page:**
```
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');
MAYBE GET DIFFERENT ARRAY SIZES AND GROUPS
$items = $_POST['items'] ?? 49;
$items = (int) $items;
if( $items > 0 && ($items < 10 || $items > 100000) ):
$items = 99999;
endif;
$title = 'Array Splice and Group';
$spf = 'https://www.sitepoint.com/community/t/generating-numbers-using-programming-language/281297/24';
$paulW = 'https://www.johns-jokes.com/downloads/sp-i/surajkay19-agroups/index-paulW.php';
$group = $_POST['group'] ?? 0;
$group = (int) $group;
if( $group < 3 || $group > 100):
$group = 15;
endif;
# $item = 99999;
# $group = 15;
$array = range(1, $items);
shuffle($array);
$row = '';
$id = 0;
for( $offset = 0; $offset < count($array); $offset += $group):
$tmp = $array;
$tmp = array_slice($tmp, $offset, $group);
$row .= formatGroup($tmp, ++$id);
endfor;
// CREATE AND DISPLAY WEB PAGE
$table = <<< ____TMP
<table class="dib fss">
<tr>
<th class="tal"> id </th><th> Groups </th>
</tr>
$row
</table>
____TMP;
$page = <<< ____TMP
<!doctype html>
<html lang="en">
<head>
<title> $title </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body {background-color:#900; color:snow;}
hr {clear:both;}
td {width:3em;}
.dib {display: inline-block;}
.fgy {color:#ff0;}
.flr {float:right;}
.fss {font-size: small;} .fsl {font-size: x-large;}
.mg1 {margin: 1em auto 0;}
.ooo {margin:0; padding:0;}
.tac {text-align:center;} .tal {text-align:left;}
.w88 {width:88%; max-width:888px;}
</style>
<body class="ooo">
<h3 class="flr ooo fgy"> <a href="$spf"> <b class="fgy">SP Forum </b> </a> </h3>
<h1> $title </h1>
<h3 class="flr ooo fgy"> <a href="$paulW"> <b class="fgy"> Paul Wilkins </b> </a> </h3>
<hr>
<form class="tal fsl" action="?" method="post">
<label> Items: </label>
<input type="text" name="items" value="$items" />
<label> Groups: </label>
<input type="text" name="group" value="$group" />
<input type="submit" value="Submit">
</form>
<div class="w88 mg1 tac fss">
$table
</div>
<hr>
</body>
</html>
____TMP;
echo $page;
//====================================
function formatGroup($array=, $id=0)
{
$max = count($array) -1;
$result = ‘’;
$result .= ‘#’ .$id .‘’;
$result .= ‘’;
foreach( $array as $item ):
$comma = ($id < $max ) ? ‘,’ : ‘’;
$result .= $item .$comma;
endforeach;
$result .= ‘’;
$result .= ‘’;
return $result;
}
//====================================
function fred($val)
{
echo ‘
’;
print_r($val);
echo ‘
’;
}
**Edit:**
Fixed HTML Validation Error and some spellings :(
Thanks to everyone who posted. Especially @coothead @John_Betong @Mittineague @James_Hibbard Etc.
Can someone upload the source code to their server an provide a link?
Did not work for me. Just tried it. Have a bunch of errors
Its working Now. Sorry about that. But it is not generating all the possible numbers. See screen shot:
Try adjusting the array and group values that are outside the minimum and maximum input values.
1 Like
It would help others if you show the script you changed.
1 Like
system
Closed
70
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.