Get substring value with defined length using python - python-2.7

I'd like to make a program in python to slice the string with length 23. I have a code that solves the purpose. I would like to know if there is a better and simple way to do this?
prtNum = "88-90-25-65-13-20-20-41-88-90-25-65-13-20-20-41-88-91-31-81-81-20-20-44-88-91-31-81-82-20-20-44-88-91-31-82-81-20-20-44-88-91-31-81-83-20-20-44"
len = len(prtNum)
length = 23
start = 0
for i in range(0, len):
sw = prtNum[start:length]
len1 = length + 1
start = len1
length = length+ 24
print(sw)
output looks like this:
88-90-25-65-13-20-20-41
88-90-25-65-13-20-20-41
88-91-31-81-81-20-20-44
88-91-31-81-82-20-20-44
88-91-31-82-81-20-20-44
88-91-31-81-83-20-20-44

Related

Running multiple arrays in SAS

I am trying to run 3 arrays in my SAS code and input each value into the variables in the last array, however, each time I run this code, it only populates the CREVASC_age column. Please let me know any thoughts on how to populate each age variable, in the third array, using the matching variables in the other arrays.
data Outc_adjust3; set Outc_adjust2;
ARRAY outcvars{3} CABG MI CREVASC;
ARRAY outcdys{3} CABGDY MIDY CREVASCDY;
ARRAY outc_age{3} CABG_age MI_age CREVASC_age;
Do I= 1 to 3;
if outcvars{3} = 1 then outc_age{3} = ageatenroll + (outcdys(3)/365.25);
else if outcvars{3} = 0 then do;
if EXTFLAG = 0 AND EXT2FLAG = 0 then outc_age{3} = AGE_WHIENDFU;
if EXTFLAG = 1 AND EXT2FLAG = 0 then outc_age{3} = AGE_EXT1ENDFU;
if EXT2FLAG = 1 AND EXT2MRC = 1 then outc_age{3} = age_endfu;
if EXT2FLAG = 1 AND EXT2SRC = 1 then outc_age{3} = age_ext1endfu;
end;
end;
run;
You hard coded {3} when you needed {I}. That is why only the third variable of the array was being dealt with.
Change the {3} to {I} for all the array indexed references inside the loop.

Find starting and ending index of each unique charcters in a string in python

I have a string with characters repeated. My Job is to find starting Index and ending index of each unique characters in that string. Below is my code.
import re
x = "aaabbbbcc"
xs = set(x)
for item in xs:
mo = re.search(item,x)
flag = item
m = mo.start()
n = mo.end()
print(flag,m,n)
Output :
a 0 1
b 3 4
c 7 8
Here the end index of the characters are not correct. I understand why it's happening but how can I pass the character to be matched dynamically to the regex search function. For instance if I hardcode the character in the search function it provides the desired output
x = 'aabbbbccc'
xs = set(x)
mo = re.search("[b]+",x)
flag = item
m = mo.start()
n = mo.end()
print(flag,m,n)
output:
b 2 5
The above function is providing correct result but here I can't pass the characters to be matched dynamically.
It will be really a help if someone can let me know how to achieve this any hint will also do. Thanks in advance
String literal formatting to the rescue:
import re
x = "aaabbbbcc"
xs = set(x)
for item in xs:
# for patterns better use raw strings - and format the letter into it
mo = re.search(fr"{item}+",x) # fr and rf work both :) its a raw formatted literal
flag = item
m = mo.start()
n = mo.end()
print(flag,m,n) # fix upper limit by n-1
Output:
a 0 3 # you do see that the upper limit is off by 1?
b 3 7 # see above for fix
c 7 9
Your pattern does not need the [] around the letter - you are matching just one anyhow.
Without regex1:
x = "aaabbbbcc"
last_ch = x[0]
start_idx = 0
# process the remainder
for idx,ch in enumerate(x[1:],1):
if last_ch == ch:
continue
else:
print(last_ch,start_idx, idx-1)
last_ch = ch
start_idx = idx
print(ch,start_idx,idx)
output:
a 0 2 # not off by 1
b 3 6
c 7 8
1RegEx: And now you have 2 problems...
Looking at the output, I'm guessing that another option would be,
import re
x = "aaabbbbcc"
xs = re.findall(r"((.)\2*)", x)
start = 0
output = ''
for item in xs:
end = start + len(item[0])
output += (f"{item[1]} {start} {end}\n")
start = end
print(output)
Output
a 0 3
b 3 7
c 7 9
I think it'll be in the Order of N, you can likely benchmark it though, if you like.
import re, time
timer_on = time.time()
for i in range(10000000):
x = "aabbbbccc"
xs = re.findall(r"((.)\2*)", x)
start = 0
output = ''
for item in xs:
end = start + len(item[0])
output += (f"{item[1]} {start} {end}\n")
start = end
timer_off = time.time()
timer_total = timer_off - timer_on
print(timer_total)

NZEC error in Python - SPOJ

def multiply(num1, num2):
result = num1 * num2
print(result)
return result
tc = raw_input("") #testcases
tc = [int(tc)]
#count = [int(count)]
count = 0
while ( count < tc) :
var1, var2 = raw_input("").split()
var1, var2 = [int(var1), int(var2)]
multiply (var1,var2)
print '\n'`**enter code here**`
count = count + 1
I'm getting NZEC on SPOJ . I'm new to python programming .
Please help me. Thanks in advance . Link to the Question on SPOJ
There are two issues in you code:
Line 7: tc = [int(tc)]
You shouldn't wrap variable tv into the list. You have created a singleton with the number of test cases as an element. Variable tc should be a number, therefore, the line should look like this:
tc = int(tc)
Line 14: print '\n'**enter code here**
You should remove the weird part "**enter code here**".
After mentioned changes your code should be a correct solution to the MUL problem on Spoj.

Informix 9.52C1 - replace carriage return and line feed

I search google and couldn't find anything that really helped me IBM website show Informix 11.5 and up functions, like if they have stop supporting 9.52C1 or something. Hence the reason I am here.
As the topic stated I'm using Informix 9.52C1. I got this information using the SQL:
SELECT owner FROM systables WHERE TABNAME= ' VERSION';
I'm not sure if the replace function is even supported because when I execute the statement:
SELECT col1, REPLACE('r','p','poster') as col2 FROM table;
The column col2 contains just the letter 'r' however no error was thrown although I used the REPLACE() function.
I know the escape characters (CRLF) can be found using the "\r\n" escape format because this SQL worked accordingly:
SELECT * FROM table WHERE col1 LIKE '%\r\n%';
Note: I used the above SQL to retrieve the version number as I only had access via a client DB application. If however you have access to the actual host running the informix DB server I'm sure there is some command arguement to resolve the version maybe the defualt linux --version or -v arguement with the db server application.
And this indeed returned only the records that contained CRLF.
My main job is to place a backslash in front of CR and LF as I am migrating data from Informix 9.52C1 to a PostgreSQL database. I'm planning to use the COPY function to load the data to the PostgreSQL database and the COPY function works in this manner as I have done a test record. My dilemma is extracting the data from Informix in the correct format. Can anyone assist with this issue?
I have tried:
SELECT REPLACE('\\\r\\\n','\r\n',col1) as description FROM table;
However this didn't work I believe due to the replace function, as I have mention I am not sure if the replace function is available in this Informix version.
Thanks in advance,
jerg
P.S. None of the functions ASCII(),CHAR() and CHR() worked either. Some sites suggested these function. But as far as I can see the functions CHR() & ASCII() was implemented in verison 11.5+. This post suggested the functions.
To migrate data between databases you can use JDBC drivers (I use Jython, but Java or other language which can use JDBC driver will be ok) and then SELECT ... from source database (Informix) and INSERT ... just read data into destination (PostgreSQL). With BatchInsert or PreparedStatement it is really fast. In my code it looks like:
insert_str = 'INSERT INTO ' + table_name + ' (' + column_names + ') VALUES (' + question_marks + ')'
insert_stmt = db_to.prepareStatement(insert_str)
...
pstm2 = db_from.createStatement()
rs_in = pstm2.executeQuery('SELECT %s FROM %s %s' % (column_names, table_name, order_str))
...
while (rs_in.next()):
for i in range(1, col_count + 1):
insert_stmt.setObject(i, rs_in.getObject(i))
Of course you must take care of errors, auto commit, fetch size etc. but I think it is worth your effort.
Your idea with COPY ... is also good. Do you save output from Informix in text file? If it is in text file you can simply correct text file before importing it to PostgreSQL.
I don't recognize the version 9.52C1. An Informix version might be 9.52.UC1 — with a choice from U, F, W, H, T for the first letter. But then again, the main version numbers were 9.00..9.03, 9.10..9.16, 9.20, 9.21, 9.30, 9.40, 10.00, 11.10, 11.50, 11.70, 12.10 — a sequence which doesn't include a 9 and a 5 in a single version. Well, in many ways, it doesn't matter; any version 9.x has been out of support for most of a decade, if not longer, and shouldn't still be in use.
Assuming that an upgrade to a modern version of Informix is not in the cards, you are stuck with implementing your own functions. To do the job, you'd do best to implement them in C and install them as a package of UDRs (user-defined routines) in a shared library. That would give you the best performance. Assuming that's not feasible (I don't have a set of such functions on hand, but it shouldn't be hard to write them), then you have to fall back on SPL (stored procedure language) routines. These are not fast because of the limitations of SPL w.r.t substring operations — specifically, the built-in substring operations with [] notation do not accept variables as subscripts. That's really painful, to be polite about it.
Here's a version of char_at which fetches a single character at a given position in a string of up to 255 characters:
-- #(#)$Id: char_at.spl,v 1.1 1999/05/17 23:59:59 jleffler Exp $
--
-- CHAR_AT stored procedure, to return character at given position in string
--
-- Author: J Leffler
-- Date: 1999-05-17
CREATE PROCEDURE char_at(str VARCHAR(255), pos SMALLINT) RETURNING CHAR(1);
DEFINE c CHAR(1);
IF pos > LENGTH(str) OR pos <= 0 THEN
LET c = NULL;
ELIF pos <= 16 THEN
IF pos = 1 THEN LET c = str[ 1];
ELIF pos = 2 THEN LET c = str[ 2];
ELIF pos = 3 THEN LET c = str[ 3];
ELIF pos = 4 THEN LET c = str[ 4];
ELIF pos = 5 THEN LET c = str[ 5];
ELIF pos = 6 THEN LET c = str[ 6];
ELIF pos = 7 THEN LET c = str[ 7];
ELIF pos = 8 THEN LET c = str[ 8];
ELIF pos = 9 THEN LET c = str[ 9];
ELIF pos = 10 THEN LET c = str[10];
ELIF pos = 11 THEN LET c = str[11];
ELIF pos = 12 THEN LET c = str[12];
ELIF pos = 13 THEN LET c = str[13];
ELIF pos = 14 THEN LET c = str[14];
ELIF pos = 15 THEN LET c = str[15];
ELIF pos = 16 THEN LET c = str[16];
END IF;
ELIF pos <= 32 THEN LET c = char_at(str[ 17, 32], pos - 1 * 16);
ELIF pos <= 48 THEN LET c = char_at(str[ 33, 48], pos - 2 * 16);
ELIF pos <= 64 THEN LET c = char_at(str[ 49, 64], pos - 3 * 16);
ELIF pos <= 80 THEN LET c = char_at(str[ 65, 80], pos - 4 * 16);
ELIF pos <= 96 THEN LET c = char_at(str[ 81, 96], pos - 5 * 16);
ELIF pos <= 112 THEN LET c = char_at(str[ 97,112], pos - 6 * 16);
ELIF pos <= 128 THEN LET c = char_at(str[113,128], pos - 7 * 16);
ELIF pos <= 144 THEN LET c = char_at(str[129,144], pos - 8 * 16);
ELIF pos <= 160 THEN LET c = char_at(str[145,160], pos - 9 * 16);
ELIF pos <= 176 THEN LET c = char_at(str[161,176], pos - 10 * 16);
ELIF pos <= 192 THEN LET c = char_at(str[177,192], pos - 11 * 16);
ELIF pos <= 208 THEN LET c = char_at(str[193,208], pos - 12 * 16);
ELIF pos <= 224 THEN LET c = char_at(str[209,224], pos - 13 * 16);
ELIF pos <= 240 THEN LET c = char_at(str[225,240], pos - 14 * 16);
ELIF pos <= 255 THEN LET c = char_at(str[241,255], pos - 15 * 16); -- Note asymmetry in upper bound!
ELSE LET c = NULL; -- Not reached!
END IF;
RETURN c;
END PROCEDURE;
Handling general substrings and the like is similarly painful. I've never gone to the effort of building even a semi-performant SPL implementation of SUBSTR. It is far, far better to upgrade to a version of Informix that has the support built-in.
CHR() and ASCII()
The CHR(). ASCII() functions can be simulated with:
-- #(#)$Id: chr.sql,v 1.2 2008/09/19 18:48:37 jleffler Exp $
--
-- #(#)Procedure CHR() - return character corresponding to integer
CREATE PROCEDURE chr(i INTEGER) RETURNING CHAR(1) AS result;
DEFINE c CHAR;
IF i < 0 OR i > 255 THEN
RAISE EXCEPTION -746, 0, 'CHR(): integer value out of range 0..255';
END IF;
IF i = 0 OR i IS NULL THEN
LET c = NULL;
ELSE
SELECT chr INTO c FROM ascii WHERE val = i;
END IF;
RETURN c;
END PROCEDURE;
-- #(#)$Id: ascii.sql,v 1.2 2008/09/19 18:40:19 jleffler Exp $
--
-- Procedure ASCII - returning integer corresponding to character.
-- Misnomer: it works on any single-byte character.
CREATE PROCEDURE jl_ascii(C CHAR) RETURNING INT AS result;
DEFINE i INTEGER;
IF c IS NULL THEN
LET i = 0;
ELSE
SELECT val INTO i FROM ascii WHERE chr = c;
END IF;
RETURN i;
END PROCEDURE;
which requires a table:
-- #(#)$Id: asciitbl.sql,v 1.2 2005/03/30 17:51:12 jleffler Exp $
--
-- #(#)Create ASCII Table (for ASCII and CHR functions).
CREATE TABLE ascii
(
val INTEGER NOT NULL UNIQUE CONSTRAINT u1_ascii,
chr CHAR(1) NOT NULL UNIQUE CONSTRAINT u2_ascii
);
REVOKE ALL ON ascii FROM PUBLIC;
GRANT SELECT ON ascii TO PUBLIC;
and the data to go in it — 255 lines such as:
…
32|
33|!
34|"
35|#
36|$
37|%
38|&
39|'
40|(
…
64|#
65|A
66|B
67|C
68|D
69|E
…
You should be able to find a copy of this code at the IIUG under the name ascii.

Variable within a number

This code ask for a message and a value to the user and then it modifies it with the given value. The problem is that I want the ASCII codes to not go over 126 or under 33, I tried to do so in the highlighted part of the code but when the value of the ASCII code gets over 126 the code returns me nothing for some reason.
loop = True
def start():
final_word = []
word_to_crypt = str(raw_input("Type a word: "))
crypt_value = int(raw_input("Choose a number to cript yout message with: "))
ascii_code = 0
n = 0
m = len(word_to_crypt)
m = int(m - 1)
while n <= m:
ascii_code = ord(word_to_crypt[n])
ascii_code += crypt_value
############# highlight #############
if 33 > ascii_code > 126:
ascii_code = (ascii_code%94)+33
############# highlight #############
final_word.append(chr(ascii_code))
n += 1
print 'Your crypted word is: ' + ''.join(final_word)
while loop:
start()
Sorry if it's not formatted well or for any mistakes in my explanation but I'm on my phone and I'm not native
Solved thank you very much this site and this community is helping me a lot!
There is no number that is greater than 126 and less than 33 at the same time. It should be:
if 33 < ascii_code < 126:
Edit:
If you want the reversed case, you will have to do it separately:
if ascii_code < 33 or ascii_code > 126:
Or you can just use the in operator and a list:
if ascii_code not in [33,126]: