Skip to content

Instantly share code, notes, and snippets.

@sakadonohito
sakadonohito / 0_reuse_code.js
Last active August 29, 2015 14:22
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@sakadonohito
sakadonohito / pascal.py
Created December 8, 2013 13:24
pythonでパスカルの三角形
#!/usr/bin/python
if __name__ == '__main__':
num = raw_input()
num = int(num) if num else 10
l = [1]
print l
for i in range(num):
l = map(lambda x,y:x+y,[0]+l,l+[0])
@sakadonohito
sakadonohito / pascal.rb
Created December 8, 2013 11:55
パスカルの三角形?
#!/usr/bin/ruby
#--*-- coding: utf-8 --*--
num = gets.chomp.to_i
num ||= 10
a=[1]
num.times{
a=[1]+a.zip(a.slice(1..(a.size))).map{|a,b| a.to_i+b.to_i}
p a.join(' ')
@sakadonohito
sakadonohito / unique_string4.php
Created December 6, 2013 13:57
与えられた文字列が全てユニークかどうか判定する。その4
<?php
//文字列を配列に変換し、1文字ずつスライスしながら重複チェックする
function check_unique($str){
//get length
$len = strlen($str);
//change array
$arr = str_split($str);
for($i=0;$i<$len;$i++){
$char = array_shift($arr);
@sakadonohito
sakadonohito / unique_string3.php
Created December 6, 2013 13:56
与えられた文字列が全てユニークかどうか判定する。その3
<?php
//文字列のまま重複チェック
function check_unique($str){
$len = strlen($str);
for($i=0;$i<$len;$i++){
$char = substr($str,$i,1);
//検査対象の文字が文字列のどの位置にあるかを前からと後ろから走査して文字位置が違うとFalse(uniqueではない)
if(strpos($str,$char) != strrpos($str,$char)){
return "False";
}
@sakadonohito
sakadonohito / unique_string1.php
Created December 6, 2013 13:53
与えられた文字列が全てユニークかどうか判定する。その1
<?php
function check_unique($str){
//引数から配列を生成
$arr = str_split($str);
$length = count($arr);
//1文字づつ比較
for($i=0;$i<$length;$i++){
$tmp = $arr;
@sakadonohito
sakadonohito / unique_string2.php
Last active December 30, 2015 10:58
与えられた文字列が全てユニークかどうか判定する。その2
<?php
function check_unique($str){
//引数から1文字1要素の配列を生成
$arr = str_split($str);
//配列の値をキーにする配列を生成
$tmp = array_combine($arr,$arr);
//引数から生成した配列の要素数とその配列から生成した配列の要素数を比較(キーは重複できないので)
return count($arr) == count($tmp)?"True":"False";
}
@sakadonohito
sakadonohito / email_attachment.php
Last active December 28, 2015 05:38
FuelPHPのEmailパッケージを使ってファイルを添付する時の文字化け対策
<?php
public function sendmail($mail_info){
$email = \Email::forge();
$mail->from($mail_info['from']);
$mail->to($mail_info['to']);
$mail->cc($mail_info['cc']);
$mail->subject(mb_convert_kana($mail_info['subject'],"KV"));//件名に半角カナが含まれた場合を考慮して全角カナに変換
//最初の引数はファイルを見つけるためにも使うのでそのまま、通常は第1引数のみ。文字化け対策に別名用引数の第5引数にエンコードした添付ファイル名を渡す
if(array_key_exists("attach",$mail_info)) $mail->attach($file_info['attach'],false,null,null,mb_convert_encoding($file_info['attach'],"ISO-2022-JP","CP932"));
@sakadonohito
sakadonohito / fuelphp_upload_register.php
Created November 13, 2013 14:58
FuelPHP Upload::register(event,callback);
<?php
/**
Upload::save()を行う前にアップロードするファイル名をエンコードするイケナイ方法
ファイル名に含まれている半角カナを全角に変換し、さらに内部エンコードからCP932にエンコード
**/
Upload::register('before',function($file){
$file['filename'] = mb_convert_encoding(mb_convert_kana($file['filename'],"KV"),"CP932");
});
@sakadonohito
sakadonohito / print.css
Created August 25, 2013 04:32
印刷用css
@media print {
/* 印刷時に非表示にする要素の指定をする */
body.print div.print-none ,
body.print div.navbar,
body.print hr,
body.print div.footer,
body.print span#anken-toggle-mark {
display: none;
}