targetURL := "http://www.example.com/one/two/three.json?output=four"
u, err := url.Parse(targetURL)
if err != nil {
panic(err)
}
fmt.Println(path.Base(u.Path))
// three.json
Example to download a file from url and save on local
io.Copy()
read 32kb (maximum) from input and write to output reapeatly, so dont need worry about memory.
func DownloadFile(filepath string, url string) error {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
reference: Code Example
var fibTests = []struct {
n int // input
expected int // expected result
}{
{1, 1},
{2, 1},
{3, 2},
{4, 3},
{5, 5},
{6, 8},
{7, 13},
}
source: 5 simple tips and tricks for writing unit tests in #golang
First of all, PHP Code Example. (PHP ≥ 7)
function isStriaghtFlush($d) {
$b = $f = 0;
$m = ['a'=>1,1=>10,'j'=>11,'q'=>12,'k'=>13];
foreach ($d as $a) {
$p = substr($a,-1);
$b |= 1<<(($m[$a[0]])??$a[0]);
$f = ($f===0||$f===$p) ? $p : 1;
}
$b = ($b << 3) | ($b >> 10);
foreach (range(0,13) as $i) {
if ((($b >> $i) & 31) == 31){
return [true, $f!=1];
}
}
return [false, false];
}
Explain:
A
can be straight of A, 2, 3, 4, 5
and 10, J, Q, K A
There are total 14 ranks, when getting rank n
from the $d
, we set 1 of the n
element in bitwise map.
If we could find any 5 continuously 1 in the bitwise map, then it’s straight. also if all faces are same, then it’s straight flush.
Example, cards: 10, J, Q, K, A
read 10
, bitwise map: 0,0,0,1,0,0,0,0,0,0,0,0,0,0
read J
, bitwise map: 0,0,1,1,0,0,0,0,0,0,0,0,0,0
read Q
, bitwise map: 0,1,1,1,0,0,0,0,0,0,0,0,0,0
read K
, bitwise map: 1,1,1,1,0,0,0,0,0,0,0,0,0,0
read A
, bitwise map: 1,1,1,1,0,0,0,0,0,0,0,0,1,0
shift left 3 of bitwise map to 1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0
shift right 10 of bitwise map to 1,1,1,1
OR this two bitwise maps and get a new map 1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1
then we have 5 continuously 1, it’s straight.
Using Homebrew to install MariaDB, and got this eror when starting the server
$ mysql.server start
Starting MySQL
.161227 19:05:10 mysqld_safe Logging to '/usr/local/var/mysql/xxx.err'.
ERROR!
Mostly it is caused by previous installation of MySQL.
Manually remove /usr/local/var/mysql
and reinstall MariaDB,
it will start normally.
You will get this kind of error message when you run brew services
in tmux
/usr/local/Cellar/something/homebrew.mxcl.something.plist: Operation not permitted
How to fix it, just need install reattach-to-user-namespace
brew install reattach-to-user-namespace
Date Range Overlap
Only 2 conditions that overlap does not exist
|------ Date Range A ------|
|------ Date Range B ------|
or
|------ Date Range A ------|
|------ Date Range B ------|
That means start date A is later than end date B, or end data A is early than start date B.
Overlap exists if neither of them is true.
In rails we can create a scope to find all overlaps
scope :overlaps, -\>(start_date_, end_date_) do
where “((start_date \<= ?) and (end_date \>= ?))", end_date_, start_date_
end
reference:
http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
http://baodad.blogspot.com.au/2014/06/date-range-overlap.html
Ruby on Rails datepicker
Use bootstrap datepicker gem
Gemfile:
gem 'bootstrap-datepicker-rails'
Add this line to app/assets/stylesheets/application.scss
*= require bootstrap-datepicker3
Add this line to app/assets/javascripts/application.js
//= require bootstrap-datepicker
Add this line to template
<%= f.text_field :publish_at, "data-provide" => 'datepicker', "data-date-format" => "yyyy-mm-dd" %>
Install Arch Linux in Virtualbox
https://wiki.archlinux.org/index.php/Installation_guide
Pre-installation
Partition the disks
$ fdisk /dev/sda
boot
> n
> return (default primary)
> return (default partition 1)
> return (default first sector)
> +250M
swap
> n
> return (default primary)
> return (default partition 2)
> return (default first sector)
> +2G
/
> n
> return (default primary)
> return (default partition 3)
> return (default first sector)
> +2G
> return
/home
> n
> p (make primary)
> return (default first sector)
> return (default last sector)
make /dev/sda1 bootable
> a
> 1
make /dev/sda1 to swap type partition
> t
> 2
> 82
write table to disk and exit fdisk
> w
Format the partitions
format partitions
$ mkfs.ext4 /dev/sda1
$ mkfs.ext4 /dev/sda3
$ mkfs.ext4 /dev/sda4
make swap
$ mkswap /dev/sda2
mount swap
$ swapon /dev/sda2
Mount the partitions
$ mount /dev/sda3 /mnt
$ cd /mnt
$ mkdir boot home
$ mount /dev/sda1 boot
$ mount /dev/sda4 home
$ cd /
Installation
Select the mirrors
rankmirrors to make this faster (though it takes a while)
mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.orig
rankmirrors -n 6 /etc/pacman.d/mirrorlist.orig \>/etc/pacman.d/mirrorlist
pacman -Syy
Install the base packages
# install base packages (take a coffee break if you have slow internet)
pacstrap /mnt base base-devel
Configure the system
Generate an fstab file
$ genfstab -p /mnt >> /mnt/etc/fstab
Change root into the new system
$ arch-chroot /mnt
Set the hostname
$ echo [archlinux] > /etc/hostname
Set the time zone
$ ln -sf /usr/share/zoneinfo/Australia/Sydney /etc/localtime
uncomment the needed locales in /etc/locale.gen
, then generate them with
$ locale-gen
Set locale preferences in /etc/locale.conf
and possibly $HOME/.config/locale.conf
$ echo LANG=en_US.UTF-8 > /etc/locale.conf
Set the root password (root account ,not your account, we will create that later)
$ passwd
Install a bootloader
$ pacman -S grub-bios
$ grub-install /dev/sda
Create a new initial RAM disk
$ mkinitcpio -p linux
$ grub-mkconfig -o /boot/grub/grub.cfg
$ exit
Unmount the partitions
$ umount /mnt/home
$ umount /mnt/boot
$ umout /mnt
$ reboot
Until not, we finish installing archlinux, then we are going to setup the system.
Post-installation
After reboot, you might not be able to connect to innternet, run command to get ip address
$ dhcpcd
run it automatically when system starts
$ systemctl enable dhcpcd
turn on multi lib to install 32bit applications
$ vi /etc/pacman.conf
uncomment
[multilib](#)
Include = /etc/pacman.d/mirrorlist
update packages
$ pacman -Syy
$ pacman -Su
install x environment
$ pacman -S xorg-server xorg-xinit xorg-server-utils
intall mesa for 3d
$ pacman -S mesa
install virtualbox guest packages
$ pacman -S virtualbox-guest-utils
$ modprobe -a vboxguest vboxsf vboxvideo
create
$ vi /etc/modules-load.d/virtualbox.conf
add
vboxguest
vboxsf
vboxvideo
$ reboot
$ pacman -S xorg-twm xorg-xclock xterm
$ startx
create user to login
$ useradd -m -g users -G storage,power,wheel -s /bin/bash jma
$ passwd jma
$ visudo
allow user to run sudo
command
find Uncomment to allow members of group wheel to execute any command
and uncomment %wheel ALL=(ALL) ALL