1. Recursive realization of bottle cap, bottle for bottle algorithm
1.1 Requirement Description
Each bottle of beer costs 2 yuan, and 3 empty bottles or 5 bottle caps can be exchanged for one bottle of beer. How many bottles of beer can I drink for 100 yuan? Idea: Use a recursive algorithm, buy all the beer, and then recursively calculate the cap and empty bottle can be replaced by the number of beer
1.2 Code Implementation
def extralPeer(bot: Int, cap: Int) :Int = {
val count = bot / 3 + cap / 5
if (count <= 0)
return 0
val modBot = bot % 3
val modCap = cap % 5
count + extralPeer(count + modBot, count + modCap)
}
Copy the code
Scala implements guessing games
2.1 Requirements are as follows
-
Select a battle character
-
At the beginning of the battle, the user punches and compares with the opponent, prompting the information of victory and defeat
-
Score at the end of the guessing game, one point will be added for all draws, two points will be added for victories, and no points will be added for defeats
-
Cycle against, when the input “N”, stop against, and display the results of the war
-
The score is displayed after the game is over
2.2 Implementation logic and main code
-
Create the User class User and define the attributes of the class (name, score) and the methods of the class (showFist())
-
Create the Computer class Computer, defining the attributes of the class (name, Score) and the methods of the class (showFist())
-
Realize computer random punch
-
Create a Game class and define the attributes of the class (Party A players, Party B players, times of battle)
-
Write initialization methods, game start methods
Main code:
2.3 Effect Demonstration
Code:Github.com/hulichao/bi…
def extralPeer(bot: Int, cap: Int) :Int = {
val count = bot / 3 + cap / 5
if (count <= 0)
return 0
val modBot = bot % 3
val modCap = cap % 5
count + extralPeer(count + modBot, count + modCap)
}
Copy the code
3. SQL for user location duration statistics
3.1 Requirement Description
The following data needs to be processed: Fields: User ID, Location ID, Start time, and Residence duration (minutes) 4 Example data: UserA,LocationA,8,60 UserA,LocationA,9,60 UserB,LocationB,10,60 UserB,LocationB,11,80 User UserA stays at LocationA for 60 minutes from 8 o ‘clock. Processing requirements: 1. Merge multiple consecutive records of the same user at the same location
3.2 Sql implementation
select user_id, location_id, min(start_time) as start_time, sum(stay_time) as stay_time from t1 group by user_id, location_id
Copy the code
4. Communication between actors
Write two actors, AActor and BActor, that can send messages to each other
4.1 Main Implementation
Start AActor with A start command, then send yourself A message to start the process after GO, and then A and B loop messages to each other
Main code: github.com/hulichao/bi…
object Demo {
private val MyFactory = ActorSystem("myFactory")
// Create an actor with the myFactory. actorOf method;
// Just send A message to A to start A conversation between the two actors
private val bActorRef = MyFactory.actorOf(Props[BActor]."bAcator")
private val aActorRef = MyFactory.actorOf(Props(new AActor(bActorRef)), "aAcator")
def main(args: Array[String) :Unit = {
var flag = true
while (flag) {
val consoleLine: String = StdIn.readLine()
/ / pass! To send a message
aActorRef ! consoleLine
if (consoleLine.equals("Bye bye")) {
flag = false
println("Program is coming to an end!")}// Hibernate for 100 milliseconds
Thread.sleep(100)}}}Copy the code
5. Simulate the communication between the Master and the Worker process in Spark
www.jianshu.com/p/43cf21b42…
To better understand the HeartBeat mechanism of the master and slave services, the communication between the master and slave is simulated.
-
The Worker registers with the Master, the Master completes the registration and replies to the Worker that the registration is successful (registration function)
-
The Worker periodically sends the heartbeat and receives it from the Master
-
After receiving the Worker’s heartbeat, the Master needs to update the last heartbeat sending time of the Worker
-
Start a scheduled task for the Master to periodically detect which registered workers do not update their heartbeats and delete them from the Hashmap
5.1 Code Implementation
Key points: Both Worker and Master are started by sending start, and the protocol is realized by using template classes. Therefore, several case classes are added. The processing logic pays attention to which types of messages the client sends and which types of messages the server receives. Github.com/hulichao/bi… park
Main achievements:
//master message processing
val workers = mutable.Map[String.WorkerInfo] ()override def receive: Receive = {
case "start" => {
println("master running....")
// Check the timeout worker
self ! StartTimeOutWorker
}
case StartTimeOutWorker => {
println("start check timeout worker...")
// Define a timer to check whether the worker's heartbeat times out at regular intervals
import context.dispatcher
context.system.scheduler.schedule(0 millis, 9000 millis, self, RemoveTimeOutWorker)}case RemoveTimeOutWorker= > {// Get all workerInfo of workers
val workerInfos = workers.values
// Get the current time
val currentTime = System.currentTimeMillis()
// Find the worker whose timeout is 6 seconds
workerInfos.filter(info => (currentTime - info.lastHeartBeatTime) > 6000)
.foreach(workers -= _.id)
println(s"===> workers.size = ${workers.size}")}case RegisterWorkerInfo(id, cpu, ram) => {
// Check whether the user is registered
if(! workers.contains(id)) {val info =
// Add data
workers += (id -> new WorkerInfo(id, cpu, ram)) // The worker list is added
println("Workers => register :" + workers)
// The registration is successful
sender() ! RegisteredWorkerInfo}}case HeartBeat(id) => {
// Update the heartbeat time of workinfo
// Retrieve workerInfo from workers
val workerInfo = workers(id)
// Update the last heartbeat time
workerInfo.lastHeartBeatTime = System.currentTimeMillis()
println(s"master updated worker ${id}'s heartbeat")}Copy the code
// Worker message processing
override def receive: Receive = {
case "start" => {
println("worker running...")
// Send the registration information
masterProxy ! RegisterWorkerInfo(id, 16.16 * 1024)}case RegisteredWorkerInfo => {
println(s"worker ${id} registered!")
// Define a timer to tell yourself to send heartbeat at regular intervals
import context.dispatcher
// 0 millis: execute immediately
// 3000 millis: execute every 3 seconds
// self: receive the object and send it to yourself
// SendHeartBeat: Send the content
context.system.scheduler.schedule(0 millis, 3000 millis, self, SendHeartBeat)}// Send heartbeat
case SendHeartBeat => {
println(s"worker ${id} send heartbeat to master")
masterProxy ! HeartBeat(id)
}
Copy the code
5.3 presentation
Check your profile for more.