Hi, I'm new to python so I'm asking a noob question.
I'm given a large string from which I have to take all the digits and check if its prime or not. Then I have to take the first 25 chars of the string and append the prime digits to it. How am I supposed to do it ?

Recommended Answers

All 8 Replies

Can you give a generic example of such a string and the expected result ?

Some help ...
prime numbers are only divisible by unity and themselves
integers less than 2 and even numbers other than 2 are not prime

s = "hi!"
for i in s:
    #do something
    pass #put pass if no code

Example :

Long String : cwl8yv0wxuo?8v#wxa#yte3m0vr#5tdbt$k?j7ovzg$@$62nk@6c0$s#bbsmu1gqu7na85u3zl9903#518ukv2awgk0u#attjehlbu?o5y#c5yw83b2n@kq8$nq4fgvf9pqj0ejwbj87a33$0nqb0h04@yf0y7i4qgoq?qeik80be3z4iwqrb?sbbsrwa58k#cd9yt8g?k04jb0opdkflngpepl1rvtlw1z8j9p#kw$mzv$2p4ep$ycl8r3u5$hvvfz1ny7vn2vq3a?hoje@czgqxghqccasm432ovlh8m9m010nt3zv#l2n@8?5h#jyw?f99l5e5e$7w87g8xfgc#$n7961xobpe315np1ht?dvqpe?h#@r81o823iyh1so35vhjmzc2x#3?pkjqh1sy5gc3sp$qvka5a2kcdpasfj@9b7dqvfwgucc5$xkaz3jxp4m#yu?s2j#hf9ul@tg?beeo@p4hn$wm#f0ev26b7k8g$zz4cps$z9qv95sxg5jwwnedu@yhq2ldfw#0#siesfrrzgo##zz52rfk$7ot97mxl#v9re$qgnoxfowgmg?q2lw3gtq7uxyo9ceosko58caw@82#awxvd3ezou#tzv?6sx9csy1w3h?d#z7kido

Answer : dxmzwxyvp@w$xyb$zufn111435

My code in C :

# include <stdio.h>
# include <math.h>

int prime_check ( int i )
{
    int j = 0;

    for(j=2; j <= sqrt(i); j++ )
        if(!(i%j)) break;


    if (j>sqrt(i))
        return 1 ;
    else
        return 0 ;
}

int main (void)
{
    int n, i, j = 0, prime = 0, composite = 0;
    char arr[50], num[20] ;
    char str[] = {That HUGE LONG string} ;

    for ( i = 0; str[i] != '\0'; i++ )
    {
        if ( isdigit(str[i]) )
        {
            n = str[i] - '0' ;
            if ( n != 1 && n != 0)
            {

                if ( prime_check (n) == 1 )
                    prime += n ;
                else
                    composite += n ;
            }
        }

        else
        if ( (i < 25) && (isdigit (str[i]) == 0) )
            arr[j++] = str[i] + 1 ;
    }

    itoa ( prime*composite, num, 10 ) ;

    strcat ( arr, num ) ;

    printf ("%s", arr ) ;

    return 0;
}

I'm new in python, so I'm not being able to write an equivalent code for this.

I was trying to do thatarr[j++] = str[i] + 1 ; part. My python code was arr+str(int(i)+1)). It says invalid literal for int() base 10. What should I do ? My objective is to take the each char from string ( upto first 25 chars ) and give them unit positive shift.

Here is my version for python 2

#!/usr/bin/env python
#-*-coding: utf8-*-

def prime_check(n):
    '''
    check if integer n is a prime, return True or False
    '''
    # 2 is the only even prime
    if n == 2:
        return True
    # integers less than 2 and even numbers other than 2 are not prime
    elif n < 2 or not n & 1:
        return False
    # loop looks at odd numbers 3, 5, 7, ... to sqrt(n)
    for i in range(3, int(n**0.5)+1, 2):
        if n % i == 0:
            return False
    return True

def main():
    prime = 0
    composite = 0;
    from array import array
    arr = ''
    src = array('b', (
        'cwl8yv0wxuo?8v#wxa#yte3m0vr#5tdbt$k?j'
        '7ovzg$@$62nk@6c0$s#bbsmu1gqu7na85u3zl'
        '9903#518ukv2awgk0u#attjehlbu?o5y#c5yw'
        '83b2n@kq8$nq4fgvf9pqj0ejwbj87a33$0nqb'
        '0h04@yf0y7i4qgoq?qeik80be3z4iwqrb?sbb'
        'srwa58k#cd9yt8g?k04jb0opdkflngpepl1rv'
        'tlw1z8j9p#kw$mzv$2p4ep$ycl8r3u5$hvvfz'
        '1ny7vn2vq3a?hoje@czgqxghqccasm432ovlh'
        '8m9m010nt3zv#l2n@8?5h#jyw?f99l5e5e$7w'
        '87g8xfgc#$n7961xobpe315np1ht?dvqpe?h#'
        '@r81o823iyh1so35vhjmzc2x#3?pkjqh1sy5g'
        'c3sp$qvka5a2kcdpasfj@9b7dqvfwgucc5$xk'
        'az3jxp4m#yu?s2j#hf9ul@tg?beeo@p4hn$wm'
        '#f0ev26b7k8g$zz4cps$z9qv95sxg5jwwnedu'
        '@yhq2ldfw#0#siesfrrzgo##zz52rfk$7ot97'
        'mxl#v9re$qgnoxfowgmg?q2lw3gtq7uxyo9ce'
        'osko58caw@82#awxvd3ezou#tzv?6sx9csy1w'
        '3h?d#z7kido'
    ))
    ord_zero = ord('0')

    def isdigit(n):
        return 0 <= n - ord_zero < 10

    for i, x in enumerate(src):
        if isdigit(x):
            n = x - ord_zero
            if n > 1:
                if prime_check(n): # if n in (2, 3, 5, 7):
                    prime += n
                else:
                    composite += n
        elif i < 25:
            arr += chr(x + 1)

    arr += str(prime * composite)

    print(arr)

if __name__ == "__main__":
    main()

""" my output -->
dxmzwxyvp@w$xyb$zufn111435
"""

Instead of a byte string, we use an array of small integers from module array (in python 3, we'd use the byte type, which is a read-only array of small integers). For prime_check(), we use Vegaseat's code

I have error in this line ans += str(i + 1). It says Can't convert 'int' object to str implicitly. I declared i = "", before this.

Why did you declare i = "" ? Python cannot add a str to an int.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.