I want to using case on if statement, can I use it? Because I always got error when i compile it ;w;
Error I got:
Tahun.pas(26,21) Fatal: Syntax error, ";" expected but "ELSE" found
Tahun.pas(0) Fatal: Compilation aborted
Here my code:
uses Crt;
var
sisa, bulan, tahun : integer;
begin
ClrScr;
writeln('masukkan tahun'); read(tahun);
sisa := tahun mod 4;
if sisa =0 then
writeln('masukkan bulan 1-12'), read(bulan);
case bulan of
1: write('31');
2: write('29');
3: write('31');
4: write('30');
5: write('31');
6: write('30');
7: write('31');
8: write('30');
9: write('31');
10: write('30');
11: write('31');
12: write('30');
else write('bulan tidak lebih dari 12');
end;
else
writeln('masukkan bulan 1-12'), read(bulan);
case bulan of
1: write('31');
2: write('28');
3: write('31');
4: write('30');
5: write('31');
6: write('30');
7: write('31');
8: write('30');
9: write('31');
10: write('30');
11: write('31');
12: write('30');
else write('bulan tidak lebih dari 12')
end;
readln;
readln;
end.
Or maybe you know how to improve the code? ;w;
Thank you for answering ;w;
I hope you have read the links upon the pieces of advice given in the comments yesterday. So there are several possible answers to the question:
The 1st – to repair your code:
begin
writeln('masukkan tahun');
readln(tahun);
writeln('masukkan bulan 1-12');
readln(bulan);
case bulan of
1, 3, 5, 7, 8, 10, 12: writeln('31');
2: if tahun mod 4 = 0 then
writeln('29')
else
writeln('28');
4, 6, 9, 11: writeln('30');
else
write('bulan tidak lebih dari 12');
end;
readln;
end.
2nd – to optimize it:
const
DinM: array [boolean, 1 .. 12] of byte =
((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
begin
writeln('masukkan tahun');
readln(tahun);
writeln('masukkan bulan 1-12');
readln(bulan);
writeln(DinM[tahun mod 4 = 0, bulan]);
readln;
end.
And 3rd:
Use predesigned functions: Delphi has a function DaysInAMonth described here.
Lazarus has it as well.
Note
Remember that all these methods (including standard functions) will give an error calculating leap years as not all years that year mod 4 = 0 are leap. For example year 1700, 1800, 1900, 2100 etc. are not leap.
Related
Example: List of List =
[
[4, 175.52, 00, 175.52, 175.52],
[3, 175.52, 01, 175.52, 175.52]
]
1 = A
2 = B
3 = C
4 = D
And
00 = AA
01 = BB
12 = CC
15 = DD
I know I can use for loop and replace example in first item like:
4 to D and 00 to AA
Expected result:
[
[D, 175.52, AA, 175.52, 175.52],
[C, 175.52, BB, 175.52, 175.52]
]
So, How to replace List item value with certain condition?
This is a weird question so I'm not sure if this is what you're looking for:
void main() {
List<List<num>> bigList = [
[4, 175.52, 00, 175.52, 175.52],
[3, 175.52, 01, 175.52, 175.52]
];
List<List<dynamic>> newBigList = [];
for (List<num> smallList in bigList) {
List<dynamic> newSmallList = [];
for (num number in smallList) {
switch (number) {
case 1:
newSmallList.add("A");
break;
case 2:
newSmallList.add("B");
break;
case 3:
newSmallList.add("C");
break;
case 4:
newSmallList.add("D");
break;
case 00:
newSmallList.add("AA");
break;
case 01:
newSmallList.add("BB");
break;
case 12:
newSmallList.add("CC");
break;
case 15:
newSmallList.add("DD");
break;
default:
newSmallList.add(number);
}
}
newBigList.add(newSmallList);
}
print(newBigList);
}
This prints:
[[D, 175.52, AA, 175.52, 175.52], [C, 175.52, A, 175.52, 175.52]]
My question was replace the first (item(0)) and third (item(2)) in the this list. [4, 175.52, 00, 175.52, 175.52]. above post #mans gave me the correct answer. Differences is that in his solution you can replace every item in list if it meets the certain condition. In my problem is similar but replace only 2 items. I use if else statement before switch condition to get what I need.
void main() {
List<List<dynamic>> bigList = [
[2, 175.52, 00, 175.52, 175.52],
[3, 175.52, 01, 175.52, 175.52]
];
List<List<dynamic>> newBigList = [];
for (List<dynamic> smallList in bigList) {
List<dynamic> newSmallList = [];
for (var number in smallList) {
if (number == smallList[0]) {
switch (number) {
case 1:
newSmallList.add("A");
break;
case 2:
newSmallList.add("B");
break;
case 3:
newSmallList.add("C");
break;
case 4:
newSmallList.add("D");
break;
default:
newSmallList.add(number);
}
} else if (number == smallList[2]) {
switch (number) {
case 00:
newSmallList.add("AA");
break;
case 01:
newSmallList.add("BB");
break;
case 12:
newSmallList.add("CC");
break;
case 15:
newSmallList.add("DD");
break;
default:
newSmallList.add(number);
}
} else {
newSmallList.add(number);
}
// if else
} // inner for
newBigList.add(newSmallList);
} // outer for
print(newBigList);
}
I have the below dictionary and I attempted to remove duplicates using the below piece of code.
vertex = {1: (4.0,7.0), 2: (1.0,4.0), 3: (5.0,8.0), 4: (5.0,6.0), 5: (3.0,8.0), 6: (4.0,7.0), 7: (1.0,4.0), 8: (5.0,8.0), 9: (4.0,2.0), 10: (4.0,8.0), 11: (4.0,7.0), 12: (4.0,2.0), 13: (4.0,8.0), 14: (1.0,4.0), 15: (5.0,8.0), 16: (4.0,4.0), 17: (4.0,2.0), 18: (4.0,8.0), 19: (2.0,2.0), 20: (5.0,5.0), 21: (4.0,7.0), 22: (4.0,2.0), 23: (4.0,8.0), 24: (5.0,6.0), 25: (3.0,8.0)}
result = {}
for key,value in vertex.items():
if value not in result.values():
result[key] = value
print result
The duplicate values are still getting added to result.
[] (4.0,7.0) - initial result and value to be checked if its already in result
[(4.0,7.0)] (1.0,4.0)
[(4.0,7.0), (1.0,4.0)] (5.0,8.0)
[(4.0,7.0), (1.0,4.0), (5.0,8.0)] (5.0,6.0)
[(4.0,7.0), (1.0,4.0), (5.0,8.0), (5.0,6.0)] (3.0,8.0)
[(4.0,7.0), (1.0,4.0), (5.0,8.0), (5.0,6.0), (3.0,8.0)] (4.0,7.0) - here (4.0,7.0) is being checked and its getting added though it is already there in it as shown in next step.
[(4.0,7.0), (1.0,4.0), (5.0,8.0), (5.0,6.0), (3.0,8.0), (4.0,7.0)] (1.0,4.0)
Can anyone tell me where exactly it goes wrong? I am unable to figure that out!
Thanks.
Since you are dealing with unique tuples, set() can perform better in this case. Try this:
vertex_set = set()
results = {}
for key, value in vertex.items():
if not value in vertex_set:
result[key] = value
vertex_set.add(value)
For more on sets, refer https://docs.python.org/2/library/sets.html
I was working on my analysis code's input part, but I've got stuck because of multiple errors. Here is my unfinished code:
c sinle event analysis
implicit real(a-h,o-z)
real day(12), nmonth(12), year(12), clas(12),
$ hour(12), nmin(12), sec(12)
real mark(12)
real tst(12)
dimension D(12)
real time(2054904), proa(2054904), w1(2054904),
$ w2(2054904), w3(2054904), w4(2054904)
D(1) = 31, D(2) = 28, D(3) = 31, D(4) = 30, D(5) = 31,
$ D(6) = 30, D(7) = 31, D(8) = 31, D(9) = 30, D(10) = 31,
$ D(11) = 30, D(12) = 31
open(100,file='singleE.txt',status='OLD')
do i=1, 12
tst(i)=0
enddo
900 do i=1, 12
read(100, 1150) day(i), nmonth(i), year(i),
$ hour(i), nmin(i), sec(i), clas(i)
do j=12, 1, -1
if integer(nmonth(i)) == j then
tst(i) = tst(i) + D(j-1)
endif
enddo
tst(i) = tst(i) + day(i) + (year(i) - 2010)*365
$ + (hour(i) + nmin(i)/60)/24
if year(i) > 2011 then tst(i) = tst(i) + 1/365
endif
print *, day(i), nmonth(i), year(i), hour(i), nmin(i),
$ sec(i), clas(i), tst(i)
enddo
open(200,file='hole.dat',status='OLD')
950 FORMAT(F12.7,2x,E10.3,2x,E10.3,2x,E10.3,2x,E10.3,
$ 2x,E10.3,2x,E10.3)
1150 FORMAT(F2.0,1x,F2.0,1x,F4.0,1x,F2.0,1x,F2.0,4x,
$ F3.1)
end
The code is breaking up, I don't know why :(
Anyways, And here is the errors I've got:
singleA.f:13.6:
D(1) = 31, D(2) = 28, D(3) = 31, D(4) = 30, D(5) = 31,
1
Error: Unclassifiable statement at (1)
singleA.f:29.8:
if integer(nmonth(i)) == j then
1
Error: Unclassifiable statement at (1)
singleA.f:31.11:
endif
1
Error: Expecting END DO statement at (1)
singleA.f:38.7:
if year(i) > 2011 then tst(i) = tst(i) + 1/365
1
Error: Unclassifiable statement at (1)
singleA.f:39.10:
endif
1
Error: Expecting END DO statement at (1)
The main problem are the first and the second ones. The rest of them are, I think, caused by the second error. I know this is a long code, but It would be nice if somebody enlighten me of my mistake :)
This, and the lines like it
D(1) = 31, D(2) = 28, D(3) = 31, D(4) = 30, D(5) = 31,
is just not syntactically correct; in fact it's so broken the compiler can't figure out what's wrong and marks it as 'unclassifiable'.
The easy fix would be to put every statement on a separate line and lose the commas. Or you could replace , with ; which is the Fortran statement separator character.
I've got a list of owner names in all caps that I'd like to convert to proper capitalization:
owner1
1: DXXXXX JOSEPH V JR
2: MIRNA NXXXXX
3: ADRIAN TXXXX
4: CUTLER PXXXXXXXXX LLC
5: GVM PXXXXXXXXX LLC
6: EARLENA RXXXXXXX
7: NATHANIEL TXXXXX
8: DXXXXXX DONNA
9: LXXXX ELAINE E TR
10: SXXXXXX KIMBERLY
(for reproduction purposes:
owner1<-c("DXXXXX JOSEPH V JR","MIRNA NXXXXX","ADRIAN TXXXX",
"CUTLER PXXXXXXXXX LLC","GVM PXXXXXXXXX LLC",
"EARLENA RXXXXXXX","NATHANIEL TXXXXX","DXXXXXX DONNA",
"LXXXX ELAINE E TR","SXXXXXX KIMBERLY")
)
Desired output:
owner1
1: Dxxxxx Joseph V. Jr
2: Mirna Nxxxxx
3: Adrian Txxxx
4: Cutler Pxxxxxxxxx LLC
5: GVM Pxxxxxxxxx LLC
6: Earlena Rxxxxxxx
7: Nathaniel Txxxxx
8: Dxxxxxx Donna
9: Lxxxx Elaine E. TR
10: Sxxxxxx Kimberly
A big first step is a version of the .simpleCap function mentioned in ?chartr:
.simpleCap <- function(x) {
s <- strsplit(tolower(x), " ")[[1]]
paste(toupper(substring(s, 1, 1)), substring(s, 2),
sep = "", collapse = " ")
}
This is a large chunk of the problem, but fails on 4, 5 and 9. I can supplement this to treat key phrases (LLC, TR, etc.) separately, but this still leaves something like observation 5.
Here's the function I've got so far (sped up wonderfully by #eipi10's solution below, which vectorized the .simpleCap function, allowing the whole function to be applied to vectors):
to.proper<-function(strings){
#vectorized version of .simpleCap;
# I've also built in that I know `strings` is all caps
res<-gsub("\\b([A-Z])([A-Z]+)*","\\U\\1\\L\\2",strings,perl=T)
#In my data, some Irish/Scottish names separated the MC prefix
# Also, re-capitalize following a hyphen
res<-gsub("\\bMc\\s","Mc",gsub("(-.)","\\U\\1",res,perl=T))
for (init in c("[A-Z]","Inc","Assoc","Co",
"Jr","Sr","Tr","Bros")){
#Add a period after common abbreviations
res<-gsub(paste0("\\b(",init,")\\b"),"\\1.",res)
}
for (abbr in c("[B-DF-HJ-NP-TV-XZ][b-df-hj-np-tv-xz]{2,}",
"Pa","Ii","Iii","Iv","Lp","Tj",
"Xiv","Ll","Yml","Us")){
#Re-capitalize any string of >=3 consonants (excluding
# Y for such names as LYNN and WYNN), as well as
# some other common phrases that need upper-casing
res<-gsub(paste0("\\b(",abbr,")\\b"),"\\U\\1",res,perl=T)
}
#Re-capitalize post-Mc letters, e.g. in Mcmahon
gsub("\\bMc([a-z])","Mc\\U\\1",res,perl=T)
}
Any ideas for robust-ish ways to leave potentially unpredicted abbreviations alone in this process (particularly, like those in observation 5 which are uncommon)?
Here's a function using a Regex to convert strings to title case (adapted from #BenBolker's answer to a question I asked on SO a while back).
The function is written so that you can pass an argument called exceptions that deals with special cases like GVM. I'm not sure if this is flexible enough for your needs, since you have to hard-code the exceptions, but I thought I'd post it and see if anyone can suggest improvements.
dat = data.frame(owner1 = c("DXXXXX JOSEPH V JR","MIRNA NXXXXX","ADRIAN TXXXX",
"CUTLER PXXXXXXXXX LLC","GVM PXXXXXXXXX LLC",
"EARLENA RXXXXXXX","NATHANIEL TXXXXX","DXXXXXX DONNA",
"LXXXX ELAINE E TR","SXXXXXX KIMBERLY"))
# Convert a string to title case
tc = function(strings, exceptions="\\b(gvm)\\b") {
# Convert to title case, excluding terminal LLC, TR, etc.
title.case = gsub("\\b([a-zA-Z])([a-zA-Z]+)*( LLC| TR| FBO| LP)?",
"\\U\\1\\L\\2\\U\\3", strings, perl=TRUE)
# Add a period after initials (presumed to be any lone capital letter)
title.case = gsub(" ([A-Z]) ", " \\1\\. ", title.case)
# Deal with exceptions
title.case = gsub(exceptions, "\\U\\1", title.case, perl=TRUE, ignore.case=TRUE)
return(title.case)
}
dat$title.case = tc(dat$owner1)
owner1 title.case
1 DXXXXX JOSEPH V JR Dxxxxx Joseph V. Jr
2 MIRNA NXXXXX Mirna Nxxxxx
3 ADRIAN TXXXX Adrian Txxxx
4 CUTLER PXXXXXXXXX LLC Cutler Pxxxxxxxxx LLC
5 GVM PXXXXXXXXX LLC GVM Pxxxxxxxxx LLC
6 EARLENA RXXXXXXX Earlena Rxxxxxxx
7 NATHANIEL TXXXXX Nathaniel Txxxxx
8 DXXXXXX DONNA Dxxxxxx Donna
9 LXXXX ELAINE E TR Lxxxx Elaine E. TR
10 SXXXXXX KIMBERLY Sxxxxxx Kimberly
Whilst going through the Joy of Clojure book, I've succeedding in defining a function that, when invoked, will create and draw on a java.awt.Frame.
(defn draw-frame [f x y]
(let [frame (java.awt.Frame.)]))
However, simply defining the function in a new Leiningen REPL in a new empty Leiningen project causes the AWT framework to start. I say this, as entering the above function definition causes a new OS X 'window' to open with a 'main' menu option. If I close this window, the clojure REPL exits.
Otherwise, the function continues to behave as expected, but I'm keen to understand why this happens - creating a similar function in Java (referencing but not instantiating a java.awt.Frame) does not do exhibit the same symptoms.
Doing a bit of spelunking through rt.jar, I was able to track it down a bit.
The static initializer[1] for java.awt.Frame looks like:
0: iconst_0
1: putstatic #20 // Field nameCounter:I
4: invokestatic #114 // Method java/awt/Toolkit.loadLibraries:()V
7: invokestatic #115 // Method java/awt/GraphicsEnvironment.isHeadless:()Z
10: ifne 16
13: invokestatic #116 // Method initIDs:()V
16: new #117 // class java/awt/Frame$1
19: dup
20: invokespecial #118 // Method java/awt/Frame$1."<init>":()V
23: invokestatic #119 // Method sun/awt/AWTAccessor.setFrameAccessor:(Lsun/awt/AWTAccessor$FrameAccessor;)V
26: return
I was able to reproduce the behavior by executing java.awt.Toolkit at the REPL, so I dug into that static initializer too:
0: ldc_w #150 // class java/awt/Toolkit
3: invokevirtual #151 // Method java/lang/Class.desiredAssertionStatus:()Z
6: ifne 13
9: iconst_1
10: goto 14
13: iconst_0
14: putstatic #126 // Field $assertionsDisabled:Z
17: iconst_0
18: putstatic #84 // Field loaded:Z
21: new #152 // class java/awt/Toolkit$3
24: dup
25: invokespecial #153 // Method java/awt/Toolkit$3."<init>":()V
28: invokestatic #32 // Method java/security/AccessController.doPrivileged:(Ljava/security/PrivilegedAction;)Ljava/lang/Object;
31: pop
32: invokestatic #154 // Method loadLibraries:()V
35: invokestatic #155 // Method initAssistiveTechnologies:()V
38: invokestatic #156 // Method java/awt/GraphicsEnvironment.isHeadless:()Z
41: ifne 47
44: invokestatic #157 // Method initIDs:()V
47: return
loadLibraries seemed interesting, so that disassembly looks like:
0: getstatic #84 // Field loaded:Z
3: ifne 23
6: new #85 // class sun/security/action/LoadLibraryAction
9: dup
10: ldc #86 // String awt
12: invokespecial #87 // Method sun/security/action/LoadLibraryAction."<init>":(Ljava/lang/String;)V
15: invokestatic #32 // Method java/security/AccessController.doPrivileged:(Ljava/security/PrivilegedAction;)Ljava/lang/Object;
18: pop
19: iconst_1
20: putstatic #84 // Field loaded:Z
23: return
Rewriting the core bit of that in Clojure looks like:
(doto (sun.security.action.LoadLibraryAction. "awt")
(java.security.AccessController/doPrivileged))
Running this causes the same application to pop up, so my suspicion is that the native library has initializer code that causes it. Unfortunately, I don't feel like diving into real disassembly, so someone else will have to figure it out from that point!
[1]: I unpacked rt.jar and then used javap -p -c Foo.class to see this.