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;
}

Sending mail while user login and logout in MAC OS

Making postfix mail server start while MAC system startup

and Sending mail while user login and logout in MAC OS
=========================================================

vi /System/Library/LaunchDaemons/org.postfix.master.plist

Edit the file as bellow
————————
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>Label</key>
<string>org.postfix.master</string>
<key>KeepAlive</key>
<true/>
<key>Program</key>
<string>/usr/libexec/postfix/master</string>
<key>ProgramArguments</key>
<array>
<string>master</string>
</array>
<key>QueueDirectories</key>
<array>
<string>/var/spool/postfix/maildrop</string>
</array>
<key>AbandonProcessGroup</key>
<true/>
</dict>
</plist>

Set load the file when MAC system starts up
——————————————-
launchctl load /System/Library/LanuchDaemons/org.postfix.master.plist

Create autodaily directory
————————–
mkdir /usr/libexec/autodaily

Create the log file
———————
vi /usr/libexec/autodaily/log.log
chmod 0777 log.log

Create autodailyLogin.sh and autodailyLogout.sh file
—————————————————-
cd /usr/libexec/autodaily/
vi autodailyLogin.sh

Enter the followings in the file
——————————–
#!/bin/bash

# Created by Saiful Takeshi
# Environment variables #################################################

DEST_EMAIL=”saiful_takeshi@number-1.co.jp,hanamitsu_makoto@number-1.co.jp”

USER_NAME=”CHUMAN SHOUHEI”

TODAY=$(date +%Y/%m/%d)

EVENT_TIME=$(date “+DATE: %Y-%m-%d%nTIME: %H:%M:%S”)

LOGGEDIN_SUBJECT=”[MAC Login] : $TODAY : $USER_NAME”

LOGGEDIN_BODY=”$USER_NAME  has Logged In successfully at \n$EVENT_TIME.”

# Functions ##########

function loggedin(){

echo “$LOGGEDIN_BODY” | mail -s “$LOGGEDIN_SUBJECT” $DEST_EMAIL

return 0

}

loggedin;

echo “Login at $EVENT_TIME” >> log.log

Create autodailyLogin.sh and autodailyLogout.sh file
—————————————————-
vi /usr/libexec/autodaily/autodailyLogout.sh

Enter the followings in this file
————————————-

#!/bin/bash

# Created by Saiful Takeshi
# Environment variables #################################################

DEST_EMAIL=”saiful_takeshi@number-1.co.jp,hanamitsu_makoto@number-1.co.jp”

USER_NAME=”CHUMAN SHOUHEI”

TODAY=$(date +%Y/%m/%d)

#EVENT_TIME=$(date +%Y/%m/%d-%H:%M:%S)

EVENT_TIME=$(date “+DATE: %Y-%m-%d%nTIME: %H:%M:%S”)

LOGGEDOUT_SUBJECT=”[MAC Logout] : $TODAY : $USER_NAME”

LOGGEDOUT_BODY=”$USER_NAME san has Logged Out successfully at \n$EVENT_TIME.”

# Functions ##########

function loggedout(){

echo “$LOGGEDOUT_BODY” | mail -s “$LOGGEDOUT_SUBJECT” $DEST_EMAIL

return 0

}

loggedout;

echo “logout at $EVENT_TIME” >> log.log

Make both file as executable
———————————
chmod +x autodailyLogin.sh
chmod +x autodailyLogout.sh

Checking the script is ok or not(execute)
—————————————–
sh autodailyLogin.sh
sh autodailyLogout.sh

—————————–
Show the default setting for login and logout
———————————————
defaults read

Add the login hook event as execute the autodailyLogin.sh file when login
——————————————————————–
sudo defaults write com.apple.loginwindow LoginHook /usr/libexec/autodaily/autodailyLogin.sh

Add the logout hook event as execute the autodailyLogout.sh file when logout
——————————————————————–
sudo defaults write com.apple.loginwindow LogoutHook /usr/libexec/autodaily/autodailyLogout.sh

Now check the mac machine by login and logout

 

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

Summary command

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

#launch FTP
sudo launchctl load -w /System/Library/LaunchDaemons/ftp.plist

#copy postfix file
sudo cp /System/Library/LaunchDaemons/org.postfix.master.plist /System/Library/LaunchDaemons/org.postfix.master.plist_bk

#Edit postfix plist file
sudo vi /System/Library/LaunchDaemons/org.postfix.master.plist

#unload load postfix
sudo launchctl unload -w /System/Library/LaunchDaemons/org.postfix.master.plist
sudo launchctl load -w /System/Library/LaunchDaemons/org.postfix.master.plist

 

#autodailylogin.sh and autodailylogout.sh file

cd /usr/libexec
sudo mkdir autodaily
sudo chmod 0777 autodaily/
cd autodaily/
sudo chmod 0777 log.log

#edit files

sudo vi autodailylogin.sh

sudo vi autodailylogout.sh

#permission change to execute

sudo chmod +x autodailylogin.sh

sudo chmod +x autodailylogout.sh

Add the login hook event as execute the autodailyLogin.sh file when login
——————————————————————–
sudo defaults write com.apple.loginwindow LoginHook /usr/libexec/autodaily/autodailyLogin.sh

Add the logout hook event as execute the autodailyLogout.sh file when logout
——————————————————————–
sudo defaults write com.apple.loginwindow LogoutHook /usr/libexec/autodaily/autodailyLogout.sh

 

 

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

[confrim] button display in wordress

▼「確認」ボタンの表示の件につきまして
二つお問い合わせフォームページの場合、
page path設定方法は下記のとおりです。

‘path’ : [‘/contact/’,’/job_contact/’],

ファイル:/wp-content/themes/cs/js/js/contact-form-7-confirm.js