FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/RePAck/src/RePack.java at master · wodgithub/algorithm · GitHub
wodgithub
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
RePAck
/
src
/
RePack.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
91 lines (72 loc) · 2.25 KB
Breadcrumbs
algorithm
/
RePAck
/
src
/
RePack.java
Copy path
File metadata and controls
91 lines (72 loc) · 2.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import
java
.
util
.
LinkedList
;
import
java
.
util
.
Queue
;
import
java
.
util
.
Stack
;
/**
* Card shuffle problem. I have a card pack of 313 cards.
* I remove the topmost card from card pack and place it on desk and
* I remove the next one and put it below the pack of cards(that you are holding).
* keep doing this till all the cards are on desk.
* Pick up the card stack from desk and repeat these steps
* till you find retrieve the original cards ordered.
* I was asked to code to find how many rounds it will take to retrieve pack order.
*/
public
class
RePack
{
public
static
void
main
(
String
args
[]) {
RePack
rp
=
new
RePack
();
rp
.
init
();
rp
.
goOverAllRounds
();
}
int
capacity
=
313
;
int
countRound
=
0
;
Queue
<
Integer
>
pack
=
new
LinkedList
<
Integer
>();
Stack
<
Integer
>
desk
=
new
Stack
<
Integer
>();
int
countAction
=
0
;
boolean
done
=
false
;
public
void
init
() {
for
(
int
i
=
0
;
i
<
capacity
;
i
++) {
this
.
pack
.
add
(
i
);
}
}
public
void
goOverAllRounds
() {
while
(!
this
.
done
) {
this
.
oneRound
();
this
.
countRound
++;
if
(
this
.
done
) {
System
.
out
.
println
();
System
.
out
.
println
(
"**"
+
this
.
countAction
+
" total rounds :"
+
this
.
countRound
+
"***"
);
}
/*
for (int i = 0; i < this.capacity; i++) {
System.out.println(i + ":" + this.desk.get(i));
}
*/
if
(!
this
.
done
) {
//reload from desk
this
.
pack
.
clear
();
this
.
pack
.
addAll
(
this
.
desk
);
this
.
desk
.
clear
();
}
}
}
public
void
oneRound
() {
while
(!
this
.
pack
.
isEmpty
()) {
this
.
oneAction
();
}
}
// cleaner version
public
void
oneAction
() {
while
(
this
.
pack
.
size
() >
0
) {
this
.
countAction
++;
this
.
desk
.
add
(
this
.
pack
.
poll
());
if
(
this
.
pack
.
size
() >
0
)
this
.
pack
.
add
(
this
.
pack
.
poll
());
}
// check the order
for
(
int
i
=
0
;
i
<
this
.
capacity
; ++
i
) {
if
(
this
.
desk
.
get
(
i
) !=
i
)
return
;
}
this
.
done
=
true
;
return
;
}
}
Back
|
FazBrowse Home
|
New Git URL