Compare commits
1 Commits
7fa822fc42
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| bc2ad07dbd |
47
arduino/bluetooth_arduino.cpp
Normal file
47
arduino/bluetooth_arduino.cpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
// Basic Bluetooth sketch HC-06_01
|
||||||
|
// // Connect the Hc-06 module and communicate using the serial monitor
|
||||||
|
// //
|
||||||
|
// // The HC-06 defaults to AT mode when first powered on.
|
||||||
|
// // The default baud rate is 9600
|
||||||
|
// // The Hc-06 requires all AT commands to be in uppercase. NL+CR should not be
|
||||||
|
// added to the command string
|
||||||
|
// //
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// #include <SoftwareSerial.h>
|
||||||
|
// SoftwareSerial BTserial(2, 3); // RX | TX
|
||||||
|
// // Connect the HC-06 TX to the Arduino RX on pin 2.
|
||||||
|
// // Connect the HC-06 RX to the Arduino TX on pin 3 through a voltage
|
||||||
|
// divider.
|
||||||
|
// //
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// void setup()
|
||||||
|
// {
|
||||||
|
// Serial.begin(9600);
|
||||||
|
// Serial.println("Enter AT commands:");
|
||||||
|
//
|
||||||
|
// // HC-06 default serial speed is 9600
|
||||||
|
// BTserial.begin(9600);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// void loop()
|
||||||
|
// {
|
||||||
|
//
|
||||||
|
// // Keep reading from HC-06 and send to Arduino
|
||||||
|
// Serial Monitor
|
||||||
|
// if (BTserial.available())
|
||||||
|
// {
|
||||||
|
// Serial.write(BTserial.read());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Keep reading from
|
||||||
|
// Arduino Serial Monitor
|
||||||
|
// and send to HC-06
|
||||||
|
// if
|
||||||
|
// (Serial.available())
|
||||||
|
// {
|
||||||
|
// BTserial.write(Serial.read());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
21
arduino/bluetooth_serial_test.py
Executable file
21
arduino/bluetooth_serial_test.py
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
#
|
||||||
|
# - Pair bluetooth useing bluetoothctl.
|
||||||
|
# agent on
|
||||||
|
# pair 20:16:02:14:58:51
|
||||||
|
# - Setup serial connection:
|
||||||
|
# sudo rfcomm bind hci0 20:16:02:14:58:51 1
|
||||||
|
#
|
||||||
|
# - For module
|
||||||
|
# sudo apt-get install python-serial
|
||||||
|
#
|
||||||
|
|
||||||
|
import serial
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
bluetoothSerial = serial.Serial( "/dev/rfcomm0", baudrate=9600 )
|
||||||
|
|
||||||
|
bluetoothSerial.write( "Test from python." )
|
||||||
|
print bluetoothSerial.readline()
|
||||||
|
|
||||||
102
puzzles/interviews/training/preorder_tree_construction.cpp.orig
Normal file
102
puzzles/interviews/training/preorder_tree_construction.cpp.orig
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
|
||||||
|
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
|
||||||
|
VIM: let b:argv=""
|
||||||
|
*/
|
||||||
|
#include <vector>
|
||||||
|
<<<<<<< HEAD
|
||||||
|
#include <string>
|
||||||
|
||||||| merged common ancestors
|
||||||
|
=======
|
||||||
|
#include <stack>
|
||||||
|
>>>>>>> preorder tree construction: much better algorithm. No need to keep root.
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
struct node {
|
||||||
|
node * left = nullptr;
|
||||||
|
node * right = nullptr;
|
||||||
|
double data = 0;
|
||||||
|
|
||||||
|
node( double d )
|
||||||
|
: data(d)
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
node * prefix_to_tree( const std::vector<double>& v ){
|
||||||
|
|
||||||
|
if ( !v.size() ){
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
node * root = new node(v[0]);
|
||||||
|
|
||||||
|
std::stack<node*> stack;
|
||||||
|
stack.push(root);
|
||||||
|
|
||||||
|
for ( int i = 1; i < v.size(); ) {
|
||||||
|
if ( v[i] < stack.top()->data ){
|
||||||
|
auto n = new node(v[i++]);
|
||||||
|
stack.top()->left = n;
|
||||||
|
stack.push(n);
|
||||||
|
} else {
|
||||||
|
auto c = stack.top();
|
||||||
|
stack.pop();
|
||||||
|
while( !stack.empty() && stack.top()->data < v[i] ){
|
||||||
|
c = stack.top();
|
||||||
|
stack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto n = new node(v[i++]);
|
||||||
|
c->right = n;
|
||||||
|
stack.push(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print( node * root, const std::string& level = "" ){
|
||||||
|
if ( root ) {
|
||||||
|
print( root->left, level + " ");
|
||||||
|
std::cout << level << "- " << root->data << std::endl;
|
||||||
|
print( root->right, level + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void traverse( node * root, std::vector<double>& v ){
|
||||||
|
if ( root ) {
|
||||||
|
v.push_back(root->data);
|
||||||
|
traverse(root->left, v);
|
||||||
|
traverse(root->right, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test( const std::vector<double>& v ){
|
||||||
|
std::cout << "-----------------------------------------------------" << std::endl;
|
||||||
|
auto r = prefix_to_tree( v );
|
||||||
|
print( r );
|
||||||
|
std::vector<double> w;
|
||||||
|
traverse( r, w );
|
||||||
|
if ( v.size() != w.size() ) {
|
||||||
|
std::cout << "v.size() != w.size() => " << v.size() << " != " << w.size() << std::endl;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < v.size(); ++i ){
|
||||||
|
if (v[i]!=w[i]){
|
||||||
|
std::cout << "error at index " << i << " -> "<< v[i] << " != " << w[i] << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main( void ) {
|
||||||
|
|
||||||
|
test( {1} );
|
||||||
|
test( {2, 1, 3} );
|
||||||
|
test( {4, 2, 1, 3, 6, 5, 7} );
|
||||||
|
test( {8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15} );
|
||||||
|
test( {8, 4, 2, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15} );
|
||||||
|
test( {8, 4, 6, 5, 7, 12, 10, 9, 14, 13, 15} );
|
||||||
|
test( {8, 12, 10, 9, 11, 14, 13, } );
|
||||||
|
test( {8, 4, 2, 1, 6, 12, 10, 11, 14, 15} );
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user