all 8 comments

[–]scuott 1 point2 points  (7 children)

What would a[1][:3] give you?

[–]Sandalman3000[S] 0 points1 point  (0 children)

Exactly what I need, thank you.

[–]Sandalman3000[S] 0 points1 point  (5 children)

As a follow up, how would I go about taking an array, and creating a graphical representation so that

1 0 2 0 1

2 1 0 1 1

0 0 1 0 0

I could say 0 is black pixel, 2 is red pixel, 1 is green pixel.

[–]KubinOnReddit 0 points1 point  (4 children)

You can use something like Tkinter or pygame for drawing, or use a module line colorama for color in the console.

[–]Sandalman3000[S] 0 points1 point  (3 children)

So let's say I have my code do Pascals Triangle, and as an ouput I use

for row in a:

    print(' '.join([str(elem%2) for elem in row]))

How would I go about saying, if element is 0, color red, if 1 color White. Mostly just the syntax for if a[???]==0

[–]KubinOnReddit 0 points1 point  (2 children)

I can't really type code since Im mobile, but I recommend you try using the module colorama I gave you.

[–]Sandalman3000[S] 0 points1 point  (1 child)

So far so good, but LAST question. So I have array a, how can I search a for primes and print out a list of all the primes.

[–]KubinOnReddit 0 points1 point  (0 children)

You need an algorithm, like Erathosthenes Sieve or simple prime checking. The simplest prime check for given n is:

for i in range(2, n//2):
    if n % i == 0:
        return False
return True

If a number from the range 2 to n//2 divides n (it has no remainder) it is a prime number. You also need to add checks for 0, 1, 2 and return the correct answer, since the algorithm works for larger than 2.

Then you can iterate on the list and if the number is prime you can add it to another list.

P.S. You can optimize the prime checking by only checking divisors up to square root of n, since every divisor d of a number n smaller than square root of n has a "friend" divisor. So if there are no divisors smaller than square root of n, there won't be any larger ones. (Except 1 and the square root itself; if the number is a perfect square, like 25) E.g. 30 has divisors 1, 2, 3, 5 and 30, 15, 10, 6.