#Create sub-lists from input
1 messages · Page 1 of 1 (latest)
Entré = input
Résultat = Result (output)
The input must be on one line and the output must be displayed as the image. The values can be any length and any numbers
other examples
# input
5 1
# output
[5]
[1]
[5, 1]
# input
53 12 79 21 51 87
# output
[53]
[12]
[79]
[21]
[51]
[87]
[53, 12]
[12, 79]
[79, 21]
[21, 51]
[51, 87]
[53, 12, 79]
[12, 79, 21]
[79, 21, 51]
[21, 51, 87]
[53, 12, 79, 21]
[12, 79, 21, 51]
[79, 21, 51, 87]
[53, 12, 79, 21, 51]
[12, 79, 21, 51, 87]
[53, 12, 79, 21, 51, 87]
# input
6 1 82313
# output
[6]
[1]
[82313]
[6, 1]
[1, 82313]
[6, 1, 82313]
Create sub-lists from input
each element have 2 choices:
- Include it in the sublist
- Ignore it
No they need to be included. You need to take in input multiple numbers (on the same line) and generate a list like shown in the image.
The values can be any numbers
yea Ik, what I mean is for example in the 1st sublist only 1 is included, 2, 3, and 4 are all excluded
yea Ik
Submit where?
You can send it to me or here and obfuscated (although DM would be better for now)
oh I just noticed my idea is wrong, it's sublist I thought it as subsets 💀
One can use 2 pointers method to solve it i think the time complexity would be O(n^2) though
Ill implement it once i come home
I cant think of a dp soln though
Only 2 pointer sliding through the array would be the only soln i think
I think the solution I have in mind would be O(n^3) hmm
Do it like you can, then optimise it if necessary
yeah I'll write it when I get home xD In the meanwhile I think about it
the implementation i though of doesnt work in cpp
it might work in python using list indexing
You can do it in Python if you want too
I did this exercise in a few languages such as Python
Ive thought of another approach
will take me a day i suppose but ill do it
wont be able to do it today
have work to do
is it even possible to do it O(n^2)?
You can send it to me if you want and I might be able to give you some pointers if I can
And yes you can do it in O(n^2)
sure
My algorithm itself is O(n^3)
@proper cradle after testing both of my algorithm, it looks like my first algo is better than my second lol
I dont remember the formula to get the right exponent tho
its probably n^2ish
it's recursive like C(n,k) probability sets
||```java
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] input = inout.nextLine().split();
int i = input.length;
while (i > 0) {
for (int j = 0; j < i; j++) {
if ((input.length - i) > 0) {
System.out.print("[");
for (int k = 0; k < (input.length - i + 1); k++) {
System.out.print(input[j + k]);
if (k != (input.length - i)) {
System.out.print(", ");
}
}
System.out.println("]");
} else {
System.out.println("[" + input[j] + "]");
}
}
i--;
}
}
o(n**3) solution, iimma try to make it better brb
nvm, I'm too lazy to make it better
||
You didn't respect the input requirements tho
The input must be on one line
It's supposed to be a string that a user enters. You can't do inputs with the compiler bot but it still needs to come from a string
Ex.
String input = "5 1";
And you can simplify this
oopsie
I know it probably is possible to simplify it to be less verbose, haven't looked it that much
Hint: You can use java.util.Arrays to simplify some things and you can solve the problem with 2 for loops and no ifs.
that would be making arrays inside of that Array to consist the indexes the new list that has to be constructed by?
I don't understand what you're saying but yes?
I assume I would create like a blueprint on how to make the sublists (marked by indexes) into that Arrays object?
No
then I'm not sure what you're referring to
java.util.Arrays is a class containing utility functions for arrays.
If you use some of those, (I have 2 in mind) you can simplify everything a lot.
For the second part, you can simplify the core logic of the solution by using only 2 for loops and not ifs
obvious compilation typo errors 😂
with input and inout
;compile java
||
import java.util.*;
public class TestSubList
{
public static void printShiftList(
final List<String> list, final int amount)
{
if (list == null) { System.out.println("[]"); return; }
final int n = list.size();
if (n == amount)
{
System.out.println(list);
}
else
{
for(int i = 0; i < n; ++i)
{
final int toIndex = i + amount;
if (toIndex <= n)
{
final List<String> subList = list.subList(i, toIndex);
System.out.println(subList);
}
}
}
}
public static void printShiftList(final String input0)
{
final String input = (input0 == null) ? "" : input0.trim();
System.out.println("# input");
System.out.println(input);
final String[] arr = input.split("\\s+");
final List<String> list = Arrays.asList(arr);
System.out.println("");
System.out.println("# output");
final int n = list.size();
for(int i = 1; i <= n; ++i)
{
printShiftList(list, i);
}
System.out.println("");
}
// Test cases
public static void main(String[] args)
{
final String input1234 = "1 2 3 4";
printShiftList(input1234);
final String input53 = "53 12 79 21 51 87";
printShiftList(input53);
final String input51 = "5 1";
printShiftList(input51);
final String input82 = "6 1 82313";
printShiftList(input82);
final String inputEmpty = "";
printShiftList(inputEmpty);
final String input5 = "5";
printShiftList(input5);
}
}
||
# input
1 2 3 4
# output
[1]
[2]
[3]
[4]
[1, 2]
[2, 3]
[3, 4]
[1, 2, 3]
[2, 3, 4]
[1, 2, 3, 4]
# input
53 12 79 21 51 87
# output
[53]
[12]
[79]
[21]
[51]
[87]
[53, 12]
[12, 79]
[79, 21]
[21, 51]
[51, 87]
[53, 12, 79]
[12, 79, 21]
[79, 21, 51]
[21
Works for all use cases 🙂 and null / empty cases
You forgot to mention if you wanted just print or return List<List<String>>
If you want a Scanner version I could wrap that into also
;compile java
||
import java.util.*;
public class TestSubList2
{
public static <V> void printShiftList(final List<V> list, final int amount, final List<List<V>> output)
{
if (list == null)
{
System.out.println("[]");
output.add(Collections.<V>emptyList());
return;
}
final int n = list.size();
if (n == amount)
{
System.out.println(list);
output.add(list);
}
else
{
for(int i = 0; i < n; ++i)
{
final int toIndex = i + amount;
if (toIndex <= n)
{
final List<V> subList = list.subList(i, toIndex);
System.out.println(subList);
output.add(subList);
}
}
}
}
public static void printShiftList(final String input0)
{
final String input = (input0 == null) ? "" : input0.trim();
System.out.println("# input");
System.out.println(input);
final String[] arr = input.split("\\s+");
final List<String> list = Arrays.asList(arr);
System.out.println("");
System.out.println("# output");
final int n = list.size();
final int half = n / 2;
final int size = n * half + half;
final List< List<String> > output = new ArrayList< List<String> >(size);
for(int i = 1; i <= n; ++i)
{
printShiftList(list, i, output);
}
System.out.println("");
System.out.println("# result");
System.out.println("" + output);
System.out.println("");
assert(output.size() == size);
}
// Test cases
public static void main(String[] args)
{
final String input1234 = "1 2 3 4";
printShiftList(input1234);
final String input53 = "53 12 79 21 51 87";
printShiftList(input53);
final String input51 = "5 1";
printShiftList(input51);
final String input82 = "6 1 82313";
printShiftList(input82);
final String inputEmpty = "";
printShiftList(inputEmpty);
final String input5 = "5";
printShiftList(input5);
}
}
||
# input
1 2 3 4
# output
[1]
[2]
[3]
[4]
[1, 2]
[2, 3]
[3, 4]
[1, 2, 3]
[2, 3, 4]
[1, 2, 3, 4]
# result
[[1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4], [1, 2, 3, 4]]
# input
53 12 79 21 51 87
# output
[53]
[12]
[79]
[21]
[51]
[
Funnily, I found this criteria:
int half = n / 2;
int output_size = n * half + half;
my solution is O(n²)
Total complexity probably something like:
O(n²/2 + 3n/2)
including the splitting part.
Excluding splitting, where List<V> is provided:
O(n²/2 + n/2)
Also, subList() in JDK5+ is a "free SubList proxy class" or O(1)
i.e.: no element copy required
https://github.com/eagle518/jdk-source-code/blob/master/jdk5.0_src/j2se/src/share/classes/java/util/AbstractList.java#L860
Automatically exported from code.google.com/p/jdk-source-code - eagle518/jdk-source-code
Had less sucess with the Perl5 translation from Java 😦
;compile php
||
<?php
function printDumpArray2D($output){
print "[\n";
foreach($output as $i => $list){
print ($i == 0) ? "\t" : ",\n\t";
printDumpArray($list);
}
print "\n]\n";
}
function printDumpArray($list){
print "[ ";
print implode(', ', $list);
print " ]";
}
function printShiftListItem($list, $amount, $output){
if (!is_array($list)){
$arr = array();
printDumpArray($arr);
print("\n");
array_push($output, $arr);
return $output;
}
$n = count($list);
if ($n == $amount){
printDumpArray($list);
print("\n");
array_push($output, $list);
}else{
for($i = 0; $i < $n; ++$i){
$toIndex = $i + $amount;
if ($toIndex <= $n){
$subList = array_slice($list, $i, $amount);
printDumpArray($subList);
print("\n");
array_push($output, $subList);
}
}
}
return $output;
}
function printShiftList($input0){
$input = (!is_string($input0)) ? "" : trim($input0);
print("# input\n");
print($input);
print("\n");
$list = preg_split("/[\s,]+/", $input);
print("\n");
print("# output\n");
$n = count($list);
$half = $n / 2;
$size = $n * $half + $half;
$output = array();
for($i = 1; $i <= $n; ++$i){
$output = printShiftListItem($list, $i, $output);
}
print("\n");
print("# result");
print("\n");
printDumpArray2D($output);
print("\n");
return $output;
}
function main(){
$input1234 = "1 2 3 4";
printShiftList($input1234);
$input53 = "53 12 79 21 51 87";
printShiftList($input53);
$input51 = "5 1";
printShiftList($input51);
$input82 = "6 1 82313";
printShiftList($input82);
$inputEmpty = "";
printShiftList($inputEmpty);
$input5 = "5";
printShiftList($input5);
}
main();exit();
||
# input
1 2 3 4
# output
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 1, 2 ]
[ 2, 3 ]
[ 3, 4 ]
[ 1, 2, 3 ]
[ 2, 3, 4 ]
[ 1, 2, 3, 4 ]
# result
[
[ 1 ],
[ 2 ],
[ 3 ],
[ 4 ],
[ 1, 2 ],
[ 2, 3 ],
[ 3, 4 ],
[ 1, 2, 3 ],
[ 2, 3, 4 ],
[ 1, 2, 3, 4 ]
]
# input
53 12 79 21
||
<html><body><pre><script>
function printDumpArray2D(output){
var i=0,n=output ? +output.length : 0,list=[];
document.write("\n[\n");
for(;i<n;++i){
document.write( (i == 0) ? "\t" : ",\n\t" );
list = output[i];
printDumpArray(list,true);
}
document.write("\n]\n");
}
function printDumpArray(list,nl){
document.write("[ ");
if (list && +list.length) document.write( list.join(', ') );
document.write(" ]");
document.write(nl ? "" : "\n");
}
function printShiftListItem(list, amount, output){
if (!list){
var arr = [];
printDumpArray(arr);
output.push(arr);
return output;
}
var n = list ? +list.length : 0;
if (n == amount){
printDumpArray(list);
output.push(list);
return output;
}else{
for(var i = 0; i < n; ++i){
var toIndex = i + amount;
if (toIndex <= n){
var subList = list.slice(i, toIndex);
printDumpArray(subList);
output.push(subList);
}
}
}
return output;
}
function printShiftList(input0){
var input = !input0 ? "" : input0.replace(/^\s+|\s+$/gm,'');
document.write("# input\n");
document.write(input);
document.write("\n");
var list = input.split(/[\s,]+/);
document.write("\n");
document.write("# output\n");
var n = list ? +list.length : 0;
var half = n / 2;
var size = n * half + half;
var output = [];
for(var i = 1; i <= n; ++i){
output = printShiftListItem(list, i, output);
}
document.write("\n");
document.write("# result");
document.write("\n");
printDumpArray2D(output);
document.write("\n");
return output;
}
;!(function(){
var input1234 = "1 2 3 4";
printShiftList(input1234);
var input53 = "53 12 79 21 51 87";
printShiftList(input53);
var input51 = "5 1";
printShiftList(input51);
var input82 = "6 1 82313";
printShiftList(input82);
var inputEmpty = "";
printShiftList(inputEmpty);
var input5 = "5";
printShiftList(input5);
})();
</script>
||
JavaScript browser version 😄 (works in IE11+)
@proper cradle i dont get it
Can you expand a bit on that? What don't you understand exactly?
i dont get what the question is asking for
Okay... Basically, you're getting mulitple values as input and create a series of lists, each containing a subset of the values. The lists are generated in a specific order. Each individual number from the input sequence is output as a single-element list, then pairs of consecutive numbers, then triplets and so on until you reach a list of the size of the input values.
So, if you have 4 values, you will have 4 sub-lists of 1 value, then 3 sub-lists of 2, then 2 of 3 and finally 1 of 4
I am unsure what to add tbh. Look at the examples if you didn't, they should help.
ohhhh okay. ill do that easy
Have you been able to understand and/or do it?
i havent tried, ive been drawingnandbwatching anime. ill give it a shot next tine im feeling like coding
nice and easy
||```py
list_ = [1,4,3,6]
for i in range(len(list_)):
for n in range(len(list_)-i):
print(list_[n:n+i+1])
5 minute job
Well... As stated in the problem, it needs to initially take a user input that as to be a string with values separated by spaces which has to be formatted then passed to a function to generate the sublists. The general idea is there though.
After that, if you want a harder one you can try https://discord.com/channels/823178343943897088/1105321678667522090
Just to be sure you saw it, I edited my message @bold ferry
okay so replace line 1 with:
list_ = input()
list_.split(" ")
?
;-; well i was gonna call it list but thats a function in python
Yea, that's because calling it list is a bad name too haha
That's worst
😠i use lst all the time
Names should describe what the variable is used for or contains
And generally, they are plural
For example, a list contains user could be called users.
In this case, it contains what? Values? Elements? Numbers (although if well made it could handle other type than numbers), etc
i think i just finished the other problem
lemme test rq, i promise im listening
The point is simply use "Intention-Revealing Names"
okay gotcha
also idk where i went wrong on this other problem lmao but its WRONG
I recommend reading "clean code" by Robert C Martin. This a small overview of the naming section of it
print(smallest(261235))
print(smallest(209917))
print(smallest(285365))
print(smallest(269045))
print(smallest(296837))
[1216121315, 2, 4]
[209090107, 1, 4]
[2825232625, 0, 4]
[206090405, 3, 4]
[2926282327, 0, 4]
