A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.

    A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].

Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.
int solution(int A[], int N) {
    // return 3 for [3,2,-5,1]
    int sum_prefix = 0;
    int sum_suffix = 0;
int i, j;
for (i = 0; i < N; i++) {
    // Check if is is the equilibrium point
    sum_prefix = 0;
    for (j = 0; j < i; j++) {
       sum_prefix += A[j];
    }
    sum_suffix = 0;
    for (j = i+1; j < N; j++) {
        sum_suffix += A[j];
       // Compute suffix sum
    }
    if (sum_prefix == sum_suffix)
        return i;
}
return -1;
}

leader of this array

A non-empty zero-indexed array A consisting of N integers is given. The leader of this array is the value that occurs in more than half of the elements of A.

  $A[0] = 1;
  $A[1] = 1;
  $A[2] = 1;
  $A[3] = 50;
  $A[4] = 1;

function solution($A){
  if(empty($A)) return -1;
  
  $array_total_element = count($A);
  $ar_cnt_values = array_count_values($A);

  foreach($ar_cnt_values as $value){
      if($value > $array_total_element/2) return 1;
    }
  return -1;
}

PHPでJIS以外の文字(例:①②③)を文字化けせずに日本語メールを送る方法

mb_internal_encoding(“UTF-8”);
mail(
‘dummy@aainc.co.jp’,
mb_encode_mimeheader(‘テスト’, ‘ISO-2022-JP-MS’), //エンコードはISO-2022-JP-MSで
mb_convert_encoding(‘①②③’, ‘ISO-2022-JP-MS’), //エンコードはISO-2022-JP-MSで
“Content-Type: text/html; charset=\”ISO-2022-JP\”;\n” //ヘッダはISO-2022-JPを指定
);

jisとiso-2022-jpの違い

mb_convert_encoding関数においてiso-2022-jpとjisでは意味が違うらしい。

結論から言うとiso-2022-jpは半角カナに対応しておらずjisは対応している。
だからmb_convert_encoding($str,”jis”)と書くほうが無難。

さらにmb_encode_mimeheaderでエンコードするときもmb_encode_mimeheader($str,”jis”)と書いてあげないと半角カナが化ける結果になる。
これに気づかないとメール送信などではまる

検証

// 半角カナが無いエンコードでは同じ結果
echo base64_encode(mb_convert_encoding(“漢字ひらがなカタカナ”,“iso-2022-jp”));
// GyRCNEE7eiRSJGkkLCRKJSslPyUrJUobKEI=

echo base64_encode(mb_convert_encoding(“漢字ひらがなカタカナ”,“jis”));
// GyRCNEE7eiRSJGkkLCRKJSslPyUrJUobKEI=

// 半角カナがあるエンコードでは違う結果
echo base64_encode(mb_convert_encoding(“漢字ひらがなカタカナハンカク”,“iso-2022-jp”));
// GyRCNEE7eiRSJGkkLCRKJSslPyUrJUobKEI/Pz8/

echo base64_encode(mb_convert_encoding(“漢字ひらがなカタカナハンカク”,“jis”));
// GyRCNEE7eiRSJGkkLCRKJSslPyUrJUobKElKXTY4GyhC

// 逆の処理をするとiso-2022-jpは化ける
echo mb_convert_encoding(base64_decode(“GyRCNEE7eiRSJGkkLCRKJSslPyUrJUobKEI/Pz8/”),“utf-8”,“iso-2022-jp”);
// 漢字ひらがなカタカナ????

echo mb_convert_encoding(base64_decode(“GyRCNEE7eiRSJGkkLCRKJSslPyUrJUobKElKXTY4GyhC”),“utf-8”,“jis”);
// 漢字ひらがなカタカナハンカク

// jisにエンコードたものをiso-2022-jpからデコードすると化けない
echo mb_convert_encoding(base64_decode(“GyRCNEE7eiRSJGkkLCRKJSslPyUrJUobKElKXTY4GyhC”),“utf-8”,“iso-2022-jp”);
// 漢字ひらがなカタカナハンカク

そもそもiso-2022-jpとは

JIS規格によって規定されている日本語の文字コードの一つ。
iso-2022-jpは7ビットで表現されているため、欧米などで開発された8ビット目を無視する電子メールシステムでも問題なく使用することができる。
このため、電子メールでの日本語送受信はiso-2022-jpによって行うことが事実上の標準となっている。

DETAILS

http://www.systemexpress.co.jp/php/iso-2022-jp.html

画像認証(CAPTCHA) in php form

Captcha adding in form

=======================

index.php

——————————————

//ランダムコード取得
$rand_code = rand();
$smarty->assign(“rand_code”,$rand_code);

index.tmpl (smarty)

——————————————

<tr>
<th>画像認証<span class=”red”>※</span></th>
<td>
<img src=”../captcha/captcha_code_file.php?rand={$rand_code}” id=”captchaimg” ><br /><br />
<label for=”message”>上記の文字列を入力してください。</label><br/>
<input style=”height:20px; font-size:18px;” id=”6_letters_code” name=”6_letters_code” type=”text”>
</td>
</tr>

————————————–

check.php

——————————————–

check the match and validation of entered captcha code.

details zip file Download

 

WP text editor plugin link anchor does not work in IE 11

WPで新規投稿で編集した際、IE 11を使用するとビジュアルでのリンク設定をすることができないとき、 (edited)

 wp\wp-admin\includes\template.php 中の _wp_admin_html_begin 関数の中に meta が定義されている部分があるので、そこにレンダリングモードの切替の記述をしておいてあげれば動くみたい。

<meta http-equiv=”X-UA-Compatible” content=”IE=8, chrome=1″>