后端编程

玩踢猴子的一个约瑟夫算法

首先就给出题目了,大家先看看

一群猴子排成一圈,按1,2,…,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈,
从它后面再开始数,再数到第m只,在把它踢出去…,如此不停的进行下去,
直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程模拟此过程,输入m、n,
输出最后那个大王的编号

这个题目是我面试的公司给出的一个题目,不要标准答案,要求有自己的想法,还允许有错误,\(^o^)/~

这道题杀死我N个脑细胞,刚开始写了一种算法,在一些特定的条件下总是导致踢猴子位置的获取错误

最终还是解决了,函数如下

function getKing($n, $m){
//强制转换为数值型
$n = intval($n);
$m = intval($m);

//让猴子排好队
for($i=0; $i < $n; $i++){
$monkeys[$i] = $i+1;
}

$start = 0;//初始化开始位置
$step = $m – 1;
for($i=0; $i < $n; $i++){
//判断猴子的个数,如果只剩一个就直接返回这只猴子
$num_monkey = count($monkeys);
if($num_monkey === 1) return $monkeys[0];

//如果这一圈找不到要踢的猴子,就在下一圈找到要踢的猴子
if($start + $step >= $num_monkey){
$start = ($step+$start)%$num_monkey;
}
else{ //这一圈找到就直接踢它
$start = $start + $step;
}

unset($monkeys[$start]);    //踢猴子
sort($monkeys);        //让猴子重新站好
}
}

算法的思想就是,循环链表,但是我始终让猴子永远排成一横队,这样觉得更容易理解一点

猴子始终排成整齐的一排,中间不允许有空余的位置。

如果不用回头就能找到可以踢走的猴子,那就不回头一直在这一排往下踢

如果找了好长时间,找不到可以踢的猴子,我们还是不回头

而是重新开始从这排猴子的第一个开始找,这时候重要的是,这时候踢哪一个?

我们上次找猴子虽然没有找到,但是我们的花的功夫可不能白搭是吧,我们接着上次在找,我们知道还有几个猴子没有点名就行了,接着上一轮继续点,找到猴子,把它踢出去。

就是这样,一直循环下去,我踢踢踢,国王是不能踢的,等到踢不动了,就找到猴王了。

网上看到牛人写的算法,佩服

function kickMonkey ($n,$m) {
$s = 0;
for ($i=2; $i <=$n; $i++) {
$s = ($s+$m)%$i;
}
$win = $s+1;
return $win;
}

算法简化了很多,我还没有消化掉。

Google App Engine 301 永久重定向

由于我想把my2first.appspot.com的流量都转移到www.ddig.info上,也有搜索引擎方面的原因,所以我想把my2first.appspot.com上的内容重定向到www.ddig.info上。

1. class MainPage的get方法第一行添加

if self.request.headers["Host"] == ‘my2first.appspot.com’:

self.redirect(”http://www.ddig.info/”)

self.response.set_status(301)

2. class SinglePost的get方法

if not entries or len(entries) == 0:

return self.error(404)

的下面添加

if self.request.headers["Host"] == ‘my2first.appspot.com’:

self.redirect(”http://www.ddig.info/”+slug)

self.response.set_status(301)

3. class SitemapHandler的get方法下第一行添加

if self.request.headers["Host"] == ‘my2first.appspot.com’:

self.redirect(”http://www.ddig.info/sitemap”)

self.response.set_status(301)

上边其实也可以不加self.response.set_status(301),self.redirect方法加上第二个参数True也可以,这样是Google推荐的方法。

if self.request.headers["Host"] == ‘my2first.appspot.com’:

self.redirect(”http://www.ddig.info/sitemap”, True)

4. 注意清楚缓存和代码缩进,tinymce的空格问题真麻烦 已经解决,我换了最新版的Tinymce已经不存在这个问题。

这样基本就差不多了

以上实例只限Micolog程序。

我可不赞成这样pr作弊!

IE-Cookie跨域问题

我需要通过网站的后台嵌入iframe访问另一个网站的后台,但是两个网站的后台肯定都需要Session验证权限,Session又是基于Cookie实现的,在IE浏览器下是这样是不允许访问第三方Cookie的。

IE处于安全性和隐私考虑,默认情况下是禁止访问第三方Cookie的,我们可以通过ie浏览器的Internet 选项来设置是否允许访问第三方Cookie,但是这样只对自己有效,在别人的浏览器上就不一定行的通了。

现在介绍一种一劳永逸的方法,支持IE8。

在你创建Cookie或Session的PHP文件最上面加上

header(’P3P: CP=CAO PSA OUR’);
header(’XDomainRequestAllowed: 1′);

这是在向客户端浏览器发送两条头信息

P3P:Platform for Privacy Preferences

这样我们就可以替用户设置本页面Cookie的隐私权限了,其实和用户自己设置IE的隐私权限是一样的。

XDomainRequestAllowed:IE8新支持的特性,XDomainRequestAllowed设置为1可以实现跨域请求,这样开发Ajax就不必为跨域而烦恼了。

但是这样又为安全问题带来的隐患,使跨站攻击变的更加容易,虽然可以通过一些手段限制可以跨站的域,但是不是万不得已或者这个页面不会带来负面影响最好不要这样做。

drupal的l函数

drupal的l函数可以根据链接生成友好的url格式,自动encodeurl等,先看l函数的原型

点击这里http://api.drupal.org/api/function/l

可以看出,函数的参数$options是一个二维数组,
包括$options['attributes']和$options['html']两个一维数组,
我们常用第一个数组里边的class与title,

也就是$options['attributes']['class']、$options['attributes']['title'],
可以对a标签的class和title属性赋值。

第二个参数是路径,
l函数在生成html的时候会自动的寻找匹配的自定义url输出到html里面,

并且会自动的对url进行encodeurl,可以防止数据的丢失。

例如

<?php

$options['attributes']['title'] = ‘亲子情商训练营’;

echo l(’亲子情商训练营’, ‘/node/1′, $options);

?>

得到

<a title=”亲子情商训练营” href=”/%E6%B4%BB%E5%8A%A8/%E4%BA%B2%E5%AD%90%E6%83%85%E5%95%86%E8%AE%AD %E7%BB%83%E8%90%A5″>亲子情商训练营</a>

瞧瞧大蟒吧

一个在家确实无聊,好的电影也看的差不多了,最近心血来潮有空再看python,说它好的很多,说它差的很少,相比php是无法比的,说php不是的一搜要多少有多少。

但是相比之下,php的成功案例还是最多的

维基百科

facebook

实在举不胜数,其实优点还是很多滴

还有做为呈现结果和接受用户的旨意也是很方便的。

但我还是想要学一下python,因为我想要再学习一门语言。

ror能做什么?企业站,算了吧!

Java?很好!但是要花费太大的精力,等学会了py再说吧

.net?NO!我不喜欢,因为我希望夸平台。

那就抄起家伙,来学python吧!

Micolog添加robots.txt

Micolog是没有robot.txt的,可以当搜索引擎访问robot.txt时,总是会产生404错误。

闲着没事加上robots.txt。

做法如下

1.在static目录下创建robots.txt文件,内容如下

User-agent: *
Crawl-delay: 10

Disallow: /admin/
Disallow: /rpc/
Disallow: /myadmin/

也可以直接下载我的robots.txt

2.编辑app.yaml文件,在- url: /favicon\.ico的后面加上,下面两行记得加缩进,tinymce 自动把每行前面对空格删除了。

- url: /robots\.txt
static_files: static/robots.txt
upload: static/robots.txt

3.上传就ok了。

drupal安装tinymce编辑器

我最常用的编辑器就是fck和tinymce了,最近更喜欢tinymce了,因为fck产生的代码太多,乱,在ie下还会影响页面的显示。

但是drupal下安装tinymce并使用中文汉化的话,编辑器根本不能用,随便找来一下找到了原因。

在这里我使用的是wysiwyg api+tinymce,对tinymce的支持还是不错的。

1.下载tinymce和Language packs

http://tinymce.moxiecode.com/download.php

2.下载wysiwyg api

3.安装wysiwyg api

把模块放到sites/all/modules/下,然后启用模块。

4.创建sites/all/modules/wysiwyg/tinymce sites/all/libraries/tinymce(由于最近wysiwyg2.0正式版的更新)目录,解压缩tinymce,把jscripts文件夹放到sites/all/modules/wysiwyg/tinymce sites/all/libraries/tinymce目录下,解压缩Language packs,把里边三个文件夹覆盖到tinymce同名的文件夹下。

5.访问”站点设置“->wysiwyg下所需要的输入格式后面选择tinymce,保存就可以了,后面的“编辑”可以设置编辑器的属性。

6.新建一个节点,看到编辑器了吧,可是是E文的,想换成中文的,在上面提到的“编辑”里面有一个”basic setup”,下面有语言设置,选择中文,选哪个?先选择zh_cn吧,这个应该是对吧?

这个时候打开node编辑页面,编辑器不见了,在输入格式间怎么切换都没有用,刷新更没有用,这时候想,应该是编辑器的问题吧。

仔细观察发现Language packs文件夹下的中文语言的js文件都是以zh开头的,而wysiwyg模块的语言设置中并没有发现zh,只有zh_cn,找到原因了。

最简单的解决办法就是在sites/all/modules/wysiwyg/wysiwyg.admin.inc文件下,搜索找到”zh_cn”这个字符串所在的数组,里边添加一项”zh”,在”basic setup”里面设置语言为”zh”即可。

micolog上传favicon.ico错误

在micolog上传的过程中,会出现

Could not guess mimetype for static/images/favicon.ico. Using application/octet-stream.

但是并不影响使用

可以感觉不是那么好

其实很好解决

主要是因为在app.yaml中定义favicon.ico的路径时,忘了定义文件类型。

- url: /favicon\.ico
static_files: static/images/favicon.ico
upload: static/images/favicon.ico
mime_type: image/x-icon

这样就可以了

在GAE上搭建 Micolog博客

在此之前,甚至包括去年年底,我都曾尝试在Google App Engine 上搭建自己的博客,可是都失败了。

现在我要把我搭建的过程中出现的错误总结一下。

首先,可以参考下这篇文章http://www.kgblog.net/2009/05/30/micolog-bug.html

1.一定要把app.yaml文件的第一行

application: my2first

my2first更改为自己的id,记住中间有一空格。

先不要再自己的环境下测试,要不会生成很多编译过的文件。

2.更改index.yaml文件,这个是由于Google App Engine的更新,现在不能识别单属性的索引,解决办法就是删除单name属性的索引。

比如:

# Unused in query history — copied from input.
- kind: Archive
properties:
- name: date
direction: desc

这是我上传成功的index.yaml

indexes:

# AUTOGENERATED

# This index.yaml is automatically updated whenever the dev_appserver
# detects that a new type of query is run.  If you want to manage the
# index.yaml file manually, remove the above marker line (the line
# saying “# AUTOGENERATED”).  If you want to manage some indexes
# manually, move them above the marker line.  The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.

# Used once in query history.
- kind: Comment
properties:
- name: entry
- name: date

# Unused in query history — copied from input.
- kind: Comment
properties:
- name: entry
- name: date
direction: desc

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: categorie_keys
- name: date
direction: desc

# Used 29 times in query history.
- kind: Entry
properties:
- name: entry_parent
- name: entrytype
- name: published
- name: menu_order

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: entry_type
- name: date
direction: desc

# Used 4 times in query history.
- kind: Entry
properties:
- name: entrytype
- name: date
direction: desc

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: entrytype
- name: post_id

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: entrytype
- name: post_id
direction: desc

# Used 11 times in query history.
- kind: Entry
properties:
- name: entrytype
- name: published
- name: date
direction: desc

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: entrytype
- name: published
- name: slug
- name: date

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: entrytype=
- name: date
direction: desc

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: published
- name: date
direction: desc

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: slug
- name: date

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: tags
- name: date
direction: desc

# Unused in query history — copied from input.
- kind: Entry
properties:
- name: tags
- name: post_id
direction: desc

# Unused in query history — copied from input.
- kind: Link
properties:
- name: linktype
- name: createdate
direction: desc

# Unused in query history — copied from input.
- kind: Link
properties:
- name: linktype
- name: href

3.上传,这时候你要确定你的数据库是干净的,运行

appcfg.py update micolog。

4.打开主页,出现

A server error occurred. Please contact the administrator.

等待,这时候千万不要着急。GAE的数据库状态由Buliding 状态变为Serving状态现在需要花费时间,我就等了差不多有10分钟,当状态变为saveing时,访问首页, hello world!出现了吧。

哈哈!