Print out elements from an array with a comma between elements except the last word
By : Paddy Viswanathan
Date : March 29 2020, 07:55 AM
it helps some times Print the first word on its own if it exists. Then print the pattern as comma first, then the next element. code :
if (arrayListWords.length >= 1) {
System.out.print(arrayListWords[0]);
}
// note that i starts at 1, since we already printed the element at index 0
for (int i = 1; i < arrayListWords.length, i++) {
System.out.print(", " + arrayListWords[i]);
}
// assume String
Iterator<String> it = arrayListWords.iterator();
if (it.hasNext()) {
System.out.print(it.next());
}
while (it.hasNext()) {
System.out.print(", " + it.next());
}
|
Algorithm to remove unique elements from array and print elements in original order
By : Евгений Богуш
Date : March 29 2020, 07:55 AM
I hope this helps you . There is one question which is bugging me, and somehow, I cannot figure out what to do with it. Suppose an array {9,1,2,4,1,2,2} is given. The unique elements in the array are 9 and 4. The output array should be {1,2,1,2,2} . My idea to preserve the order and find duplicates is to use a LinkedHashMap which will have the entries and the count of occurrence of the entries. , I would do it like this:
|
How do I print the two elements of an array and the subsequent sum of these elements? (Goldbachs Primes Question)
By : user2370060
Date : March 29 2020, 07:55 AM
should help you out First things first, there are a lot of optimizations you can do to make the code logic better. the way you find primes can be improved, you only need to check numbers upto square root n to verify if n is a prime. Also, the actual verification of Goldbachs conjecture can be improved as well. However, just focusing on the current code, You should stick to using lists if you want to append values, and to stop the code you need to use a sort of hack to stop the nested looping using the for-else syntax. Also, you can use f-strings to format strings nicely from python 3.6 onwards. code :
primes = []
pairs = []
for num in range (2, 1000): #modified. you forgot 2 and 3!
for j in range (2, num):
if (num % j) == 0:
break
else:
primes.append(num)
result = []
for x in range(2,1000):
if x % 2 == 0:
for prime1 in primes:
for prime2 in primes:
if prime1 + prime2 == x:
print(f"{x} = {prime1} + {prime2}")
result.append((prime1, prime2))
break
else: #this is a for-else syntax. enter this block if the for loop did not encounter a break
continue #go to next iteration of the mid-level loop. This prevents the line afterwards from being executed in cases where the inner loop did not "break"
break #break the mid level loop if you reach this line.
else:
print("You have done it! Bingo!!")
|
print only distinct or duplicate elements of array and ignore the rest of elements - vb
By : September Inc.
Date : March 29 2020, 07:55 AM
I hope this helps you . is there a function on vb to print only the distinct elements of array, not List or ArrayList? My code is this , To find duplicates only, you can use LINQ grouping as follows: code :
Dim q = From p In TestVal
Group By p Into Group, Count()
Where Count > 1
Select p
Order By p
Console.WriteLine(String.Join(",", q)) ' will print "a,c"
Dim q = From p In TestVal
Group By p Into Group, Count()
Where Count = 1
Select p
Order By p
Console.WriteLine(String.Join(",", q)) ' will print "b"
|
Print an array elements with 10 elements on each line
By : Daye Guo
Date : March 29 2020, 07:55 AM
Any of those help I just created an array with 100 initialized values and I want to print out 10 elements on each line so it would be somthing like this
|