Python, reverse some lines down to up - python-2.7

I don't know how to do this:
a b c d e 1
a b c d e 2
a b c d e 3
...
a b c d e n
and need:
a b c d e n
....
a b c d e 3
a b c d e 2
a b c d e 1
I tried something like:
newcontent=codecs.encode(content,'utf_8','replace')
for i in reversed(newcontent):
newcontent.append(i)
print newcontent

Related

How do I get ocamlformat to ignore a specific comment

I have a comment in my code that indicates what is contained inside of a two-dimensional array:
(**
I
N H
F A
A B L A T E
B A I N T
R T H O U G H
A E A
S T
I N C I N E R A T E
V
E
*)
Due to its nature, it needs to be formatted exactly like this. However, whenever I run ocamlformat, it gets reformatted to look like this:
(** I N H F A A B L A T E B A I N T R T H O U G H A E A S T I N C I
N E R A T E V E *)
How do I fix this? The closest I've been able to get without affecting anything else is this:
let _ =
()
(*
I
N H
F A
A B L A T E
B A I N T
R T H O U G H
A E A
S T
I N C I N E R A T E
V
E
*)
[#ocamlformat "disable"]
But that's not very satisfying. This comment is supposed to be attached to a top-level item, but I can't do that with this method b/c then ocamlformat will be disabled for the entire item. I can't figure out how to disable the comment formatting only.
You can use [##ocamlformat "wrap-comments=false"] to disable only the comment-wrapping feature,
let _ =
()
(*
I
N H
F A
A B L A T E
B A I N T
R T H O U G H
A E A
S T
I N C I N E R A T E
V
E
*)
[##ocamlformat "wrap-comments=false"]
You can also disable it in .ocamlformat, but since it is already disabled by default, I think it was the intention to have it. You can also disable the feature on a module level, using [###ocamlformat "wrap-comments=false"] at the beginning of your file.
As a side note, if you want ocamldoc to preserve the look of the comment in the generated documentation, you should also wrap it in the verbatim tag, e.g., the following will be rendered verbatim,
(**
{v
If P, then Q.
Not Q.
Therefore, not P.
v}
*)

How to place the elements of a global list into a variable?

For instance, I know that I can use a global list as column names for a defined matrix, e.g.
global letter = "a b c d e f g h"
matrix colnames mymatrx = $letter
..However, I want to create a Stata variable that has the elements of my global macro within a variable, something like this:
gen myvar = $letter (Note: this doesn't work)
It is not clear exactly what you want, but all of these three interpretations are legal:
clear
set obs 8
global letter "a b c d e f g h"
gen letter1 = "$letter"
gen letter2 = "$letter" in 1
gen letter3 = word("$letter", _n)
list, sep(0)
+---------------------------------------------+
| letter1 letter2 letter3 |
|---------------------------------------------|
1. | a b c d e f g h a b c d e f g h a |
2. | a b c d e f g h b |
3. | a b c d e f g h c |
4. | a b c d e f g h d |
5. | a b c d e f g h e |
6. | a b c d e f g h f |
7. | a b c d e f g h g |
8. | a b c d e f g h h |
+---------------------------------------------+
Without the quotation marks, Stata will try to make sense of a as a variable or scalar name, and bail out if that does not work. Even if that works, it will not be able to make sense of how you want to combine it with b, and will bail out then.
In short, you usually need " " to deal with literal strings. The matrix *names commands are special, because their inputs are necessarily literal strings (even when they are numeric characters).

How do I derive a regular grammars from this regular expression?

How do I derive a regular grammars from this regular expression?
(a or b)*ba(ba)*
I'm stuck with the last part
so
S -> A | B | C
A -> aA | bA
B -> bC
C -> cD
D->bE ?
E->af | ^ ?
Any help would be appreciated thanks!
S → (a + b)* b a (b a)*
The initial (a + b)* gives three possibilities: it can expand to a (a + b)*, or it can expand to b (a + b)*, or it can be empty:
S → a (a + b)* b a (b a)* = aS
S → b (a + b)* b a (b a)* = bS
S → b a (b a)* = A
For A, the initial b a is fully constrained, so we clearly must write:
A → b a (b a)* = bB
B → a (b a)* = aC
For C, (b a)* gives two possibilities: it can expand to b a (b a)*, or it can be empty:
C → b a (b a)* = bB
C → ε
(Using the fact that a (b a)* matched a non-terminal we'd already defined. Failing that, we could instead have written:
C → b a (b a)* = bD
D → a (b a)* = aC
C → ε
.)
Putting it together:
S → aS
S → bS
S → A
A → bB
B → aC
C → bB
C → ε

Extracting words from a file in C

Could anyone help me to correct the following code. I
need to extract words (sequence of non white space characters up to a white space character or a new line character). Here the code prints each letter of extracted word 3 times.
#include<stdio.h>
#include<string.h>
main()
{
FILE *fp1,*fp2,*fp3;
char ch,str[10],lab[10],opc[10],opd[10];
int i;
fp1=fopen("ma.dat","r");
while((ch = fgetc(fp1)) != EOF)
{
i=0;
if(ch!=' ' || '\n' || -1)
{
lab[i++]=ch;
}
lab[i]='\0';
i=0;
if(ch!=' ' || '\n' || -1)
{
opc[i++]=ch;
}
opc[i]='\0';
i=0;
if(ch!=' ' || '\n' || -1)
{
opd[i++]=ch;
}
opd[i]='\0';
printf("%s %s %s ",lab,opc,opd);
}
fcloseall();
}
and here is my input :
copy start 1000
lda alpha
lda five
sta six
six word 4
alpha rword 5
five byte c'eof'
end
and the output is :
c c c o o o p p p y y y s s s t t t a a a r r r t t t 1 1 1 0 0 0 0 0 0 0 0 0
l l l d d d a a a a a a l l l p p p h h h a a a
l l l d d d a a a f f f i i i v v v e e e
s s s t t t a a a s s s i i i x x x
s s s i i i x x x w w w o o o r r r d d d 4 4 4
a a a l l l p p p h h h a a a r r r w w w o o o r r r d d d 5 5 5
f f f i i i v v v e e e b b b y y y t t t e e e c c c ' ' ' e e e o o o f f f ' ' '
e e e n n n d d d
Here I used the logic that scan until eof reached and (tried) to get get separate words until some space or newline is reached.
Do you have a debugger? Set a breakpoint and step through the program, line by line. You'll find that there is at least one statement in your loop that makes no sense. Hint: Why do you have the i=0 statement there inside the loop?
If this isn't just a typo after too many days of coding, you may want to read up on how
A while loop works. Especially which commands get repeated.
If works. Especially the difference between conditional statements like "if" and loop statements like "while".
PS - I'm obviously biased, but if you're looking for a good C tutorial, try my C tutorial http://masters-of-the-void.com - It's written for the Mac, but you already have your compiler up and running and you've compiled your own programs with it, so just doing the samples on Linux should be well within your skills.

Whitespaces in scraping results (python)

I'm trying to scrape a website with python2.7 and beautifulsoup4. The code I'm using works on one machine, on the other, I get the resulting 'soup' with three whitespaces added between the letters. I get something like the following (both in terminal as in eclipse/pydev. Any idea what's causing this?
i f ( w i n d o w . D o m L o a d e d )
{
D o m L o a d e d . l o a d ( f u n c t i o n ( ) { b a n n e r S y n c ( ' t b ' ) ; } ) ;
d o c u m e n t . w r i t e ( ' d i v i d = " d o m L o a d e d " s t y l e = " d i s p l a y : n o n e " > \ / d i v > ' ) ;
}
/ s c r i p t >
! - - S e r v e r : P h o b o s , S e r v e r t i m e : 0 , 0 9 2 7 s ( C : 0 , 0 5 2 0 ; Q : 7 ; 0 , 0 0 2 2 ; E : 5 2 ; 0 , 0 3 1 1 s , M : 3 ; 0 , 0 0 1 1 s , A : 0 ; 0 , 0 0 0 0 s ) , M e m : 1 2 3 0 1 K B , E n g i n e s : ( S ) p h o b o s ( 5 2 ) - - >
/ b o d y >
/ h t m l >
It's very possible that two machines have installed different HTML parser libraries, please check this link. As you know, different parsers may have different parse result, esp. for those ill-formed HTML.