Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can anyone help me out with this sequece.
I have tried multiple times but not getting solution. Some of the solution looks like as follows
1
11
21
1211
11121
But i need following solution. I want to print this sequece in console.
1
11
21
1211
3112
132112
311322
var last = [1, 3, 2, 1, 1, 2]
var next = [Int]()
func getNext() {
if last.count == 0 { return }
let first = last.first!
let firstCount = last.filter{ $0 == first }.count
next.append(firstCount)
next.append(first)
last.removeAll { $0 == first }
getNext()
}
getNext()
print(next)//[3, 1, 1, 3, 2, 2]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
input =>
List integerList =[1,2,3,4,30,31,50,51,100];
expecting output =>
List subLists =[[1,2,3,4],[30,31],[50,51],[100]];
basic criteria, subtraction result of each in between values of subgroups should be less than 10
This function return what you want:
List getGroupedList(List<int> integerList) {
List<List<int>> groupedList = [];
while (integerList.isNotEmpty) {
groupedList.add([integerList.first]);
integerList.removeAt(0);
while (integerList.isNotEmpty&&integerList.first - groupedList.last.last == 1) {///YOU CAN ALTER THIS 1 TO ANY NUMBER ACCORDING TO YOUR CRITERIA OF DIFFERENCE BETWEEN NUMBERS
groupedList.last.add(integerList[0]);
integerList.removeAt(0);
}
}
return groupedList;
}
OLD ANSWER
Here's an example (assuming grouping by number length):
import 'package:collection/collection.dart';
void main() {
List integerList =[1,2,3,4,50,51,100];
final result = integerList
.groupListsBy((element) => element.toString().length)
.values
.toList();
print(result); // prints: [[1, 2, 3, 4], [50, 51], [100]]
}
EDIT:
NEW ANSWER
I believe this could do what you like:
List integerList = [1,2,3,4,8,30,31,50,51,100];
final result = integerList.fold<List<List<int>>>(
[[]],
(previousValue, element) => previousValue.last.isEmpty ||
(previousValue.last.last - element).abs() < 10
? [
...previousValue.take(previousValue.length - 1),
[...previousValue.last, element]
]
: [
...previousValue,
[element]
]);
print(result); // [[1, 2, 3, 4, 8], [30, 31], [50, 51], [100]]
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 2 years ago.
Improve this question
I have done this using a repeat while loop and it ran. How can I do this with a for in loop
var salaries = [45000.0, 100000.0, 54000.0, 20000.0]
var i = 0
repeat {
salaries[i] = salaries[i] + (salaries[i] * 0.1)
i += 1
} while (i < salaries.count)
print(salaries)
You can do it using higher order function map
var salaries = [45000.0, 100000.0, 54000.0, 20000.0]
salaries = salaries.map {$0 + ($0 * 0.1)}
print(salaries)
also do it using for in loop
var salaries = [45000.0, 100000.0, 54000.0, 20000.0]
for (index,salary) in salaries.enumerated() {
salaries[index] = salary + (salary * 0.1)
}
print(salaries)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The goal here is to try and find the average of the three highest inputted grades, when four numbers are put in. I think I've come close but I'm running into a syntax error and I can't seem to find what is causing it.
I've got almost no experience with python so excuse the sloppy code but this is what I've gotten so far.
def highestThreeAvg(grade1 : float, grade2 : float, grade3 : float, grade4 : float) -> float:
"""Given the grades, grade1, grade 2, grade3, grade4, returns the average
of the three highest grades"""
return (((grade1 + grade2 + grade3 + grade4) - lowestGrade) / 3
grade1 = float(input("Enter first grade: "))
grade2 = float(input("Enter second grade: "))
grade3 = float(input("Enter third grade: "))
grade4 = float(input("Enter fourth grade: "))
def lowest(grade1, grade2, grade3, grade4):
if (grade1 < grade2) and (grade1 < grade3) and (grade1 < grade4):
lowestGrade = grade1
elif (grade2 < grade1) and (grade2 < grade3) and (grade2 < grade4):
lowestGrade = grade2
elif (grade3 < grade1) and (grade3 <grade2) and (grade3 < grade4):
lowestGrade = grade3
else:
lowestGrade = grade4
print(lowestGrade)
lowest(grade1, grade2, grade3, grade4)
print(highestThreeAvg)
The syntax error that I'm running into is at the start of line 6
grade1 = float(input("Enter first grade: "))
^
SyntaxError: invalid syntax
If you see any other issues with my code or where I can improve it feel free to let me know.
Placing an additional close bracket, as shown below, at the end of line 4 should solve the syntax issue.
return (((grade1 + grade2 + grade3 + grade4) - lowestGrade) / 3)
It seems that you have 3 (, but only 2 ) in the previous line !
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
let smallestgrade = (min(musicgrade, bildgrade, matematicsgrade)) (Dosent work)
The alternatives of grades are: "A,B,C,D,E,F"
F is the smallest value and has a value of 0
A is the biggest value and has a value of 20
how do I sort out the smallest value?
Can I do something like this:
A = 20
B = 17.5
C = 15.0
D = 12.5
E = 10
F = 0
??
Simple, type safe solution:
enum Grade : Double {
case A = 20.0
case B = 17.5
case C = 15.0
case D = 12.5
case E = 10.0
case F = 0.0
}
extension Grade : Comparable {
static func <(lhs: Grade, rhs: Grade) -> Bool {
return lhs.rawValue < rhs.rawValue
}
}
let musicGrade = Grade.A
let bildGrade = Grade.E
let mathGrade = Grade.D
let worstGrade = [musicGrade, bildGrade, mathGrade].min()
print(worstGrade)
Perhaps you want this sort of thing:
let gradeValues = ["A" : 20, "B" : 17.5, "C" : 15.0, "D" : 12.5, "E" : 10, "F" : 0]
let grades = ["B", "A", "C", "A", "B"]
let minim = grades.min {gradeValues[$0]! < gradeValues[$1]!} // "C"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can someone help me understand why the following code causes the above error?
The code is for rotation of a 2D array by 90 degrees.
def rotate90(block: Array[Array[Int]]) = {
var size: Int = block.size
var i: Int = 0
var j: Int = size - 1
while (j >= 0) {
while (i <= j) {
val a = block[j][i]
val b = block[j - i][j]
block[j][i] = b
block[j - i][j] = a
i = i + 1
}
j = j - 1
}
return block
}
def rotate90(block: Array[Array[Int]]) = {
val copy: Array[Array[Int]] = Array.ofDim[Int](block.length, block(0).length)
for (w <- 0 until block(0).length;
h <- 0 until block.length) {
copy(h).update(w, block(block(0).length - 1 - w)(h))
}
copy
}
Above is scala solution for you problem. Maybe you better understand how arrays in scala works.