why the list behaves differently in python and Java? - java

I am learning the scripting language, python. I know Java quite a bit. I am trying to translate one bit of code from Java to python. but they behave erratically (or my understanding could be totally wrong)
I have the following code in Java, where I am adding elements to a ArrayList indefinitely.
so this causes outofmemory error, which I expect:
import java.util.*;
public class Testing{
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(4);
for (int i=0;i<list.size();i++){
list.add(5);
}
}
}
now the same code translated in python:
lst = []
lst.append(5)
lst.append(4)
for i in range(len(lst)):
lst.append(5)
print lst
here I get the output: [5, 4, 5, 5]
from what I see, is the list not passed as a reference to the for loop in python?
similarly here,
>>> l=[1,2,3]
>>> for i in l[:]:
... l.append(4)
... print l
...
[1, 2, 3, 4]
[1, 2, 3, 4, 4]
[1, 2, 3, 4, 4, 4]
in each iteration inside the for loop, I am increasing the list size, so the iteration should go forever correct?

A python for loop evaluates the expression that yields the iterable over which to loop once only. You can manipulate the lst object in the loop without affecting the outcome of what for is looping over. This differs from the Java for construct (which is a very different construct from the Python for statement, which is really a Foreach construct), which evaluates the 3 associated expressions for each iteration.
In your first example, you created a range() result, and once that is created it is not updated for each loop iteration.
In your second example, you created a copy of lst using a full-length slice (lst[:]) for the loop to iterate over. The copy is not re-created for each loop iteration.
There is a caveat here, however. The for loop calls iter() on the object-to-be-iterated-over. For a list object, the resulting list iterator does keep a reference to the original list, as well as an iteration index. Every time the for loop advances the iterator (calling next() on it), the iteration index is incremented and looked up on the original list, until the index is equal to the list length. If you keep adding to the list in the loop, that would create an infinite loop.
You can see this if you do not create a copy of the list to iterate over:
>>> L = [1, 2, 3]
>>> for i in L:
... L.append(4)
... print L
... if len(L) > 30:
... break
...
[1, 2, 3, 4]
[1, 2, 3, 4, 4]
[1, 2, 3, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3,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, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
[1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
Here, the iterator created for L keeps yielding the next element as L is extended in the loop, and if I had not added a length limit in the loop this would have continued forever.

range(len(lst)) creates the range once, and then iterates over that whereas in java list.size() is evaluated at each iteration

> is the list not passed as reference to the for-loop in python?
All objects are passed by reference in Python, and in Python everything is an object. (Java primitive values are not, but even plain integer and float values are objects in Python.)
> in each iteration inside for-loop, I am increasing the list size, so the iteration should go forever correct?
You are increasing the size of l, that's correct, but l[:] is evaluated just once and produces a shallow copy of l. That copy isn't changed in the loop, so changes to l inside the loop do not change the set of values that the loop variable will take on.
Change l[:] to just l in that loop, and then you'll see a lot of output.

It may be easiest to explain this by translating the for loops into their equivalent while loops.
In Java:
for (int i=0;i<list.size();i++){
list.add(5);
}
int i=0;
while (i<list.size()) {
list.add(5);
++i;
}
In Python pseudocode:
for i in range(len(lst)):
lst.append(5)
_r = range(len(lst))
_i = iter(r)
while _i isn't done:
next(_i)
lst.append(5)
In practice, you don't really need to understand how iter* and next work, or the details of how the "_i isn't done" part works**; the key is that a for loop creates an iterator, then iterates over it. In your case, it's creating an iterator over a range object (or, in Python 2.x, the list returned by the range function).
But even without knowing that, you can see that your len(lst) is only being evaluated once at the start, to create the iterator, while the Java list.size() equivalent is being evaluated each time through the loop.
If you want the equivalent of a Java-style for loop, you have to write the while loop explicitly.
* iter creates an iterator over any iterable (a list, a range, even another iterator). An iterator is sort of like a smart object that has a reference to the iterable and a "current position" within it, although under the covers they're rarely implemented that way. Calling next on an iterator effectively returns the value at the current position and advances the iterator to the next one (or the equivalent for however the iterator is actually implemented).
** What actually happens is, in effect, a try:/except StopIteration:, because calling next on an iterator that's done raises StopIteration. Of course it's implemented in C (or Java or .NET or RPython, for other Python implementations), and the C actually uses some special magic code for looping over an iterator that makes it a little faster, but almost nobody ever needs to think about that part.

Related

How do you convert a two dimensional array of a boxed type to a two dimensional array of the corresponding primitive type?

Integer[][] documents = { {1, 4, 3, 2, 3, 1, 4, 3, 2, 3, 1, 4, 3, 2, 3, 6},
{2, 2, 4, 2, 4, 2, 2, 2, 2, 4, 2, 2},
{1, 6, 5, 6, 0, 1, 6, 5, 6, 0, 1, 6, 5, 6, 0, 0},
{5, 6, 6, 2, 3, 3, 6, 5, 6, 2, 2, 6, 5, 6, 6, 6, 0},
{2, 2, 4, 4, 4, 4, 1, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 0},
{5, 4, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2}};
int[][] convertDocument = documents;
the compiler says incompatible types: Integer[][] cannot be converted to int[][]...
how to solve this?
You have to iterate over the list and convert each element into an int primitive. Arrays are special kinds of objects in Java, so int[][] and Integer[][] are completely unrelated to each other. It would be like trying to cast a Foo to a Bar. Java does support implicit casts from Integer to int however. That's called auto-boxing, and you can see it at work below at the line primitives[i][j] = documents[i][j] where a single Integer is implicitly cast to a primitive int.
int[][] convertToPrimitives(Integer[][] documents) {
int[][] primitives = new int[documents.length][];
for (int i=0;i<documents.length;i++) {
primitives[i] = new int[documents[i].length];
for (int j=0;j<documents[i].length;j++) {
primitives[i][j] = documents[i][j];
}
}
return primitives;
}

Java; saving array of arrays into collection

So I have this data
{ { 1, 3, 5, 3, 1 },
{ 3, 5, 6, 5, 1 },
{ 7, 2, 3, 5, 0 },
{ 12, 1, 5, 3, 0 },
{ 20, 6, 3, 6, 1 },
{ 20, 7, 4, 7, 1 } }
and i want to save it into some kind of collection, list or set. So if that collection was named List,if i were to type List[0][3] it would reffer to int 4.
I tried with
ArrayList<int[]> myNumberList = new ArrayList<int[]>();
but i have trouble putting that data into list
The array access operator [] is only applicable to arrays. So you can only create 2-dimensional array.
int a[][] = new int[][]{
{1, 3, 5, 3, 1},
{3, 5, 6, 5, 1},
{7, 2, 3, 5, 0},
{12, 1, 5, 3, 0},
{20, 6, 3, 6, 1},
{20, 7, 4, 7, 1}
};
System.out.println(a[0][3]);
But you can't create any type of collection that can use [] to access it's values.
Yoy can still use List of arrays. But you will have to index first dimension, using get() method
List<int[]> a2 = Arrays.asList(
new int[]{1, 3, 5, 3, 1},
new int[]{3, 5, 6, 5, 1},
new int[]{7, 2, 3, 5, 0},
new int[]{12, 1, 5, 3, 0},
new int[]{20, 6, 3, 6, 1},
new int[]{20, 7, 4, 7, 1}
);
System.out.println(a2.get(0)[3]);
You could make it an Integer[][] and create a List<List<Integer>>. Something like,
Integer[][] arr = { { 1, 3, 5, 3, 1 }, { 3, 5, 6, 5, 1 },
{ 7, 2, 3, 5, 0 }, { 12, 1, 5, 3, 0 }, { 20, 6, 3, 6, 1 },
{ 20, 7, 4, 7, 1 } };
System.out.println(Arrays.deepToString(arr));
List<List<Integer>> al = new ArrayList<>();
for (Integer[] inArr : arr) {
al.add(Arrays.asList(inArr));
}
System.out.println(al);
which outputs (formatted for this post)
[[1, 3, 5, 3, 1], [3, 5, 6, 5, 1], [7, 2, 3, 5, 0],
[12, 1, 5, 3, 0], [20, 6, 3, 6, 1], [20, 7, 4, 7, 1]]
[[1, 3, 5, 3, 1], [3, 5, 6, 5, 1], [7, 2, 3, 5, 0],
[12, 1, 5, 3, 0], [20, 6, 3, 6, 1], [20, 7, 4, 7, 1]]
Hard to answer what do you really need in your particular case. But in general list-equivalent of 2-dimensional array, which, I guess, you are looking for, will be List<List<Integer>> type, and in java-8 you can convert it in such way:
int a[][] = new int[][]{
{1, 3, 5, 3, 1},
{3, 5, 6, 5, 1},
{7, 2, 3, 5, 0},
{12, 1, 5, 3, 0},
{20, 6, 3, 6, 1},
{20, 7, 4, 7, 1}};
List<List<Integer>> l2 = new ArrayList<>();
Stream.of(a).forEach(a1 -> l2.add(Arrays.stream(a1).boxed().collect(Collectors.toList())));

Java ArrayList clone improved run-time

This is the question:https://leetcode.com/problems/combinations/
This is my solution 1:
public class Solution {
public List<List<Integer>> combine(int n, int k){
List<List<Integer>> result = new ArrayList<List<Integer>>();
combine(n, k, 1, result, new ArrayList<Integer>());
return result;
}
public void combine(int n, int k , int start, List<List<Integer>> result, ArrayList<Integer> l){
if(k == 0){
result.add(l);
return;
}
for(int i = start; i <= n; ++i){
l.add(i);
combine(n, k - 1, i + 1, result, l);
}
}
}
Result: small test cases passed. But big test cases time exceed.
Submission Result: Time Limit Exceeded
Last executed input: 10, 5
Solution 2:
public class Solution {
public List<List<Integer>> combine(int n, int k){
List<List<Integer>> result = new ArrayList<List<Integer>>();
combine(n, k, 1, result, new ArrayList<Integer>());
return result;
}
public void combine(int n, int k , int start, List<List<Integer>> result, ArrayList<Integer> l){
if(k == 0){
result.add(l);
return;
}
for(int i = start; i <= n; ++i){
ArrayList<Integer> a = (ArrayList<Integer>) l.clone();
a.add(i);
combine(n, k - 1, i + 1, result, a);
}
}
}
Passed all test cases.
the main difference is clone of the list.
But why?
is the solution A wrong or just slow?
Why using clone is faster here?
Really confused.
The first solution is indeed incorrect. Try invoking combine(5,3), and sending it to System.out, and you'll see the output for the first is:
[[1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5], [1, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5, 2, 3, 4, 5, 4, 5, 5, 3, 4, 5, 5, 4, 5, 5]]
You'll notice that it's the same list in each index position - you really do need to create a new array each time. For the second, correct solution, the output is:
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]]
This means that the first solution is slower because you're adding numbers to a larger and larger list each time. For higher values of n and k, that list can be very large, and copying the backing array of ArrayList when it needs to expand becomes a very expensive operation - much more expensive than copying/creating a number of small lists.

Socket / Telnet-connection / BufferedReader

i have a multi-threaded notification-service which uses a simple protocol. When testing it with telnet it works fine, just one thing keeps bugging me: When i close the session by just closing the (OSX)Terminal window the BufferedReader.readLine reads some weird bytes, and doesn't throw an IOException.
This is the receiving code:
try {
/* wait for input from client */
InputStream in = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));
while (true) {
try {
/* read a line */
String line = reader.readLine();
if (line == null)
break;
L.debug("length {} bytes {}",line.length(),line.getBytes());
// do stuff
} catch (final ParseException e) {
L.error("{} parsing failed: {}", ClientConnection.this, e.getMessage());
}
}
} catch (final IOException e) {
L.error("{} IOException: {}", ClientConnection.this, e.getMessage());
}
When i close the telnet-session the code reads a line, the length is arbitray (400 or 232 or 123), and bytes[] ares always a series of 4s, like this:
2012-02-06 01:51:19,575 [pool-1-thread-5] DEBUG - as [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,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4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
Actually 4 means End of Transmission, so that should be the expected behaviour for a telnet session. (I guess it will stop sending 4s as soon as the socket really is closed)
see ascii table here: http://www.robelle.com/smugbook/ascii.html
I'm not sure why there are so many but 4 is US ASCII End of Transmission

Java array to multi dimensional

Is it possible to take an array of say 100 chars and turn it into a 2d array of 10*10?
Here you go
char[] chars = ("01234567890123456789012345678901234567890123456789" +
"01234567890123456789012345678901234567890123456789")
.toCharArray();
char[][] char2D = new char[10][10];
for (int i = 0; i < 100; i++)
char2D[i / 10][i % 10] = chars[i];
Now the this code...
System.out.println(Arrays.deepToString(char2D).replaceAll("],","],\n"));
...prints the following
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
Iterate throughout your list of 100 chars and divide it amongst the 10*10,
Modulus (%) will probably be very useful.
You could use 2 nested for loops to assign the chars of the array to the appropriate element.

Categories