Multiple integers one line without using array? - java.util.scanner

Hey guys i'm not sure if the answer is out there, I don't quite know how to describe the question I have. I've looked on here for about 45 minutes and couldn't find much that answered my question. So I apologize if it has already been posted elsewhere.
I need to be able to take a series of numbers inputted on a single line and take numbers that are only 6 - 10, and find the average and maximum of the numbers between 6 - 10.
Example : 4 5 6 6 7 7 7 8 8 8 8 9 9 9
and : 8 6 3 10
Problem is that I've found answers if you are looking to receive X amount of inputs, but the assignment I'm working on is going to input anywhere from like 1 - 20 numbers on a single line.
I cannot use Array for my assignment, I wish I could since it would make things much easier. I am completely lost as what to do.
Thanks for all the help!

If your input is a String with a space as delimeter you could do something like this
String string = "4 5 6 6 7 7 7 8 8 8 8 9 9 9";
List<String> splittedStrings = Arrays.asList(string.split(" "));
After that you have a List with the splitted input that you can process in any way you imagine (like removing unwanted entries und summing them up).
Update:
Something like this should work even without an array or list:
String string = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20";
string = string.replaceAll("16|17|17|18|19|20|[1-5](?!0)", "").trim();
System.out.println(string);
int counter = 0;
int value = 0;
int max = 0;
while (!string.isEmpty()) {
String substring = "";
if (string.indexOf(" ") == -1) {
substring = string;
string = "";
} else {
substring = string.substring(0, string.indexOf(" "));
string = string.substring(string.indexOf(" ")).trim();
}
if (!substring.isEmpty()) {
int integer = Integer.valueOf(substring).intValue();
if (integer > max) {
max = integer;
}
value = value + integer;
counter++;
}
}
System.out.println("Max: " + max + "; Average: " + Double.valueOf(value) / counter);

Related

Identify cells with Repeated strings

Sometimes a person would Type 9999999999999999 or 0000000000 8999999998888888, ...
instead of typing their identification number. I want a column to identify those cases, meaning the cases in which a single string (In this case a number string) is typed more than 7 times.
I have no idea on how to do it.. My best guess would be to count each number using len, but that would be at least 10 IF statements opened... Any suggestions?
We can find the maximum digit count as follows (as a calculated column):
MaxDigitCount =
VAR n = LEN ( [ID] )
VAR Digits =
ADDCOLUMNS ( GENERATESERIES ( 1, n ), "#D", MID ( [ID], [Value], 1 ) )
VAR Frequencies =
GROUPBY ( Digits, [#D], "#Freq", COUNTX ( CURRENTGROUP (), [Value] ) )
RETURN
MAXX ( Frequencies, [#Freq] )
Suppose [ID] = "899999999777". Then n = 12 and Digits is the table generated by creating a list from 1 to 12 and adding the column of digits corresponding to each of those positions.
Digits =
Value
#D
1
8
2
9
3
9
4
9
5
9
6
9
7
9
8
9
9
9
10
7
11
7
12
7
Then Frequencies summarizes this table by grouping on #D and counting the number of occurrences of each distinct digit.
Frequencies =
#D
#Freq
8
1
9
8
7
3
Finally, return the maximum value in the #Freq column.
Using this, it's easy to check if the value is greater than 7. You can modify the final line to
IF ( MAXX ( Frequencies, [#Freq] ) > 7, ">7", "<=7" )

Groovy to separate every 5th Character with a character

I need a groovy script to insert one separator line after every 10th unique number
Example:
Input:
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
7
8
9
7
Output:
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
///////////////
7
8
9
7
Blockquote
​def arr = [1,2,3,4,5,6,7,8,9,9,9,9,9,9,9,9,0,7,8,9,7]
def map = [:]
arr.each{
println it
map[it] = (map.containsKey("$it") ? map[it] : 1)+1
if( map.size() == 10 ){
println "/////"
map = [:]
}
}
one way:
def numbers = """\
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
7
8
9
7""".readLines().collect { it as Integer }
def unique = [] as Set
def seen = [0] as Set // 0 - don't add delimiter at start
def result = numbers.inject([]) { acc, val ->
def s = unique.size()
// if we are on a 10 multiple of unique numbers seen
// and we have not already added a delimiter for this multiple
if (!(s % 10 || s in seen)) {
acc << '///////////////'
seen << s
}
unique << val
acc << val
}
// result will now contain the original list of numbers,
// interleaved with the delimiter every 10th unique number
result.each {
println it
}
which results in:
~> groovy solution.groovy
1
2
3
4
5
6
7
8
9
9
9
9
9
9
9
9
0
///////////////
7
8
9
7

Little Book of Semaphores - barrier

I'm going through the book mentioned in the topic, and one thing bugs me. For Barrier, the solution is the following (pseudo)code:
1 rendezvous
2
3 mutex.wait ()
4 count = count + 1
5 mutex.signal()
6
7 if count == n: barrier.signal ()
8
9 barrier.wait()
10 barrier.signal()
11
12 critical point
However, should not the counter's readout be also mutex-protected, so there are no inconsistencies while reading? I mean this:
3 mutex.wait ()
4 count = count + 1
5 if count == n: barrier.signal()
6 mutex.signal()
Or am I overly cautious about the counter var maybe?
Thank you for answers.
Yup, it should be protected, and your fix looks right.
It's possible the author oversimplified their pseudocode, and actually meant this:
3 mutex.wait ()
4 count = count + 1
5 c = count
6 mutex.signal()
7
8 if c == n: barrier.signal ()
... which would be correct.

How to use AWK command for a variable with multiple entries? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I use awk to get the information from an ID with various observations/variables. As an example following, I have two IDs (a,b). Both have 6 observations at different ages with different measurements.
The file is sorted based on ID and ages.
The 'line numbers' are not the part of the actual data file but the headings are!
I'd like to use awk to identify and extract {print} the "measurement" difference between the earliest age and the latest age of every each unique ID. Looking at the following example, for ID (a), I'd like to obtain 50-11=39.
id age measurement
1 a 2 11
2 a 4 20
3 a 6 19
4 a 7 89
5 a 8 43
6 a 12 50
7 b 1 15
8 b 3 23
9 b 5 30
10 b 6 33
11 b 7 45
12 b 10 60
I would highly appreciate if you please explain in details that I could learn.
Ordered input data
Given that the line numbers are not part of the data but the headings are, the file looks more like:
id age measurement
a 2 11
a 4 20
a 6 19
a 7 89
a 8 43
a 12 50
b 1 15
b 3 23
b 5 30
b 6 33
b 7 45
b 10 60
This script analyzes that file as desired:
awk 'NR==1 { next }
$1 != id { if (id != "") print id, id_max - id_min; id = $1; id_min = $3; }
{ id_max = $3 }
END { if (id != "") print id, id_max - id_min; }' data
The first line skips the first line of the file.
The second line checks whether the ID has changed; if so, it checks whether there was an old ID, and if so, prints the data. It then stores the current ID and records the measurement for the minimum age.
The third line records the measurement for the current maximum age for the current ID.
The last line prints out the data for the last group in the file, if there was any data in the file.
Sample output:
a 39
b 45
Unordered input data
Even if the data is not sequenced by ID and age, the code can be adapted to work:
awk 'NR==1 { next }
{ if (group[$1]++ == 0)
{
order[++sequence] = $1
id_age_min[$1] = $2; id_val_min[$1] = $3
id_age_max[$1] = $2; id_val_max[$1] = $3
}
if ($2 < id_age_min[$1]) { id_age_min[$1] = $2; id_val_min[$1] = $3; }
if ($2 > id_age_max[$1]) { id_age_max[$1] = $2; id_val_max[$1] = $3; }
}
END { for (i = 1; i <= sequence; i++)
{
id = order[i];
print id, id_val_max[id] - id_val_min[id]
}
}' data
This skips the heading line, then tracks groups as they arrive and arranges to print the data in that order (group and order — and sequence). For each row, if the group has not been seen before, set up the data for the current row (the values are both the minimum and maximum).
If the age in the current row ($2) is less than the age for the current minimum age (id_min_age[$1]), record the new age and the corresponding value. If the age in the current row is larger than the age for the current maximum age (id_max_age[$1]), record the new age and the corresponding value.
At the end, for each ID in sequence, print out the ID and the difference between the maximum and minimum value for that ID.
Shuffled data:
id age measurement
a 12 50
b 10 60
a 4 20
b 3 23
b 5 30
a 7 89
b 6 33
b 7 45
a 8 43
a 6 19
a 2 11
b 1 15
Sample output:
a 39
b 45
It so happens that an a row still appeared before a b row, so the output is the same as before.
More data (note b appears before a this time):
id age measurement
b 10 60
a 12 50
a 4 20
b 3 23
c 2 19
d -9 20
d 10 31
e 10 31
b 5 30
a 7 89
b 6 33
e -9 20
b 7 45
a 8 43
a 6 19
f -9 -3
f -7 -1
g -2 -8
g -8 -3
a 2 11
b 1 15
Result:
b 45
a 39
c 0
d 11
e 11
f 2
g -5

Align two strings perfectly (python)

Is it possible to align the spaces and characters of two strings perfectly?
I have two functions, resulting in two strings.
One just adds a " " between a list of digits:
digits = 34567
new_digits = 3 4 5 6 7
The second function takes the string and prints out the index of the string, such that:
digits = 34567
index_of_digits = 1 2 3 4 5
Now the issue that I am having is when the length of the string is greater than 10, the alignment is off:
I am supposed to get something like this:
Please advice.
If your digits are in a list, you can use format to space them uniformly:
L = [3,4,2,5,6,3,6,2,5,1,4,1]
print(''.join([format(n,'3') for n in range(1,len(L)+1)]))
print(''.join([format(n,'3') for n in L]))
Or with f-string formatting (Python 3.6+):
L = [3,4,2,5,6,3,6,2,5,1,4,1]
print(''.join([f'{n+1:3}' for n in range(len(L))]))
print(''.join([f'{n:3}' for n in L]))
Output:
1 2 3 4 5 6 7 8 9 10 11 12
3 4 2 5 6 3 6 2 5 1 4 1
Ref: join, format, range, list comprehensions

Resources