Hi!
I started doing the java coding challenges from edabit.com. There is a challenge where you need to filter out Strings from an array and only leave numbers. https://edabit.com/challenge/vNszi3mbJA5BhgGFX
( turn [4,123,124,“aaa”,14,553,22,42,1,“bbb”,…] -> [4,123,124,14,553,22,42,1,…] ) what kind of data type should I use to make it? Should I just do it with strings like 4 = “4” while string “aaaaa” = ‘aaaaa’? What do you think?
How is this array given? In a text file?
I would try to store them in a generic object ArrayList, which then is easily filtered.
void setup() {
ArrayList<Object> obj_list = new ArrayList <Object>();
ArrayList<Float> numbers = new ArrayList <Float>();
obj_list.add("ddd");
obj_list.add(2);
obj_list.add(11122.33);
for (int i = 0; i < obj_list.size(); i++) {
println(obj_list.get(i));
Object obj = obj_list.get(i);
try {
Float a_float = Float.parseFloat(obj.toString());
numbers.add(a_float);
}
catch (Exception e) {
println("Not a number!");
}
}
printArray(numbers);
}
This is my Java attempt on that positive number filter challenge:
“Program.java”:
import java.util.Collection;
import java.util.LinkedHashSet;
public final class Program {
@SafeVarargs static public final String[] filterArray(final String... strs) {
if (strs == null || strs.length == 0) return new String[0];
final Collection<String> nums = new LinkedHashSet<>();
for (final String str : strs) try {
Integer.parseUnsignedInt(str);
nums.add(str);
}
catch (final NumberFormatException notPositiveNumber) {
}
return nums.toArray(new String[nums.size()]);
}
}
But b/c I haven’t created an account on Edabit.com yet I couldn’t check my code there.
However I’ve come up with this PDE sketch to test my “Program.java” file on Processing:
void setup() {
println(Program.filterArray("1", "2", "a", "b"));
println(Program.filterArray("1", "a", "b", "0", "15"));
println(Program.filterArray("1", "2", "aasf", "1", "123", "123"));
println(Program.filterArray("jsyt", "4", "yt", "6"));
println(Program.filterArray("r", "5", "y", "e", "8", "9"));
println(Program.filterArray("a", "e", "i", "o", "u"));
println(Program.filterArray("4", "z", "f", "5"));
println(Program.filterArray("abc", "123"));
println(Program.filterArray("$%^", "567", "&&&"));
println(Program.filterArray("w", "r", "u", "43", "s", "a", "76", "d", "88"));
exit();
}
1 2
1 0 15
1 2 123
4 6
5 8 9
4 5
123
567
43 76 88
I thought that the challenge was [4,123,124,“aaa”,14,553,22,42,1,“bbb”,…] , meaning not only strings in the array to filter. Anyway, I don’t know what the full challenge is, but it seems more like a P5.js mixed objects array; so a topic with the wrong tag…
If we follow the OP’s link:
Edabit.com/challenge/vNszi3mbJA5BhgGFX
The selected category is “Java”. Although we can click it to switch it for another language.
Oh, I see, but I don’t get it. This doesn’t look like a java function, with strings and numbers that way in the array. There’s something wrong with that.
Seems like those challenges are presented in some language-agnostic terms.
Then we have to solve them restricted to our chosen language syntax.
Well if we may write it as a string I would solve it this way.
import java.util.Arrays;
String str = "4,123,124,aaa,14,seila,553,22,42,1,bbb";
str = str.replaceAll("[^-?0-9]+", " ");
println(Arrays.asList(str.trim().split(" ")));
In order to check whether your code returns the expected values you’re gonna need to sign in on their site, go to the challenge, click at the “Code” tab, paste your solution and hit the “Check” button.
Thanks, but I find this forum the best place to learn.
barging into conversation but I’m honesly running out of ideas. I couldn’t get ideas for a year at some point. The projects were either too hard or too easy. It was nice to find a site that gives ideas to me. BTW do you have any interesting projects that aren’t at insane difficulty?
You might like to join the Rosetta Examples development club. Plenty of tasks to still fulfill on the Rosetta page.
Also since the beginning of december there’s the codecember event.
The idea is to do one processing sketch a day based on a concept or another artist work. This is actually really interesting and I can just encourage you to check it out!
The creators Pine and Anthony are giving great resources each day on a different subject and it’s a great way to learn new skills.
I am doing it right now but it needs more popularity
Well, finally made up my mind and made an account on Edabit.com.
And my Java solution simply worked on 1st try!
Also coded solutions for JS & Python:
JS:
function filterArray(arr) {
return arr.filter(Number.isInteger);
}
Python:
from collections import OrderedDict
def isnt_str(obj): return type(obj) is not str
def filter_list(lst): return list(filter(isnt_str, OrderedDict.fromkeys(lst)))
And this is Processing’s output test for my Python solution:
def setup():
print filter_list([ 1, 2, "a", "b" ])
print filter_list([ 1, "a", "b", 0, 15 ])
print filter_list([ 1, 2, "aasf", "1", "123", 123 ])
print filter_list([ "jsyt", 4, "yt", "6" ])
print filter_list([ "r", 5, "y", "e", 8, 9 ])
print filter_list([ "a", "e", "i", "o", "u" ])
print filter_list([ 4, "z", "f", 5 ])
print filter_list([ "abc", 123 ])
print filter_list([ "$%^", 567, "&&&" ])
print filter_list([ "w", "r", "u", 43, "s", "a", 76, "d", 88 ])
exit()
A little late to this and I am not sure if PDE has full stream support yet, but here is my java solution
public static Integer[] filterStrings(List<?> genericList) {
return genericList.stream()
.filter(entry -> entry instanceof Integer)
.toArray(Integer[]::new);
}
Then my simple demo program
public static void main(String[] args) {
List<Object> input = Arrays.asList(1, 2, 3, "aa", 'b', "ccc");
Integer[] output = filterStrings(input);
System.out.println(Arrays.toString(output));
}