Skip to content

Commit

Permalink
Create 左旋转字符串.erl
Browse files Browse the repository at this point in the history
开个Erlang的头,添加了位移字符串的递归方法。
  • Loading branch information
hejavac committed May 7, 2014
1 parent fb97e9b commit bebde86
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ebook/code/Erlang/左旋转字符串.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%%%-----------------------------------
%%% @Module : july_1_1
%%% @Author : hejavac
%%% @Email : hejavac@gmail.com
%%% @Created : 创建日期
%%% @Description : 1.1、左旋转字符串 : 字符串旋转问题,例如abcdef 左旋2位 变成 cdefab
%%%-----------------------------------

-module(july_1_1).

-compile(export_all).

%% 启动
%% Str:字符串
%% Step:位移量
%% 返回:位移后的字符串
start(Str, Step) ->
shift(Str, Step).

%% 字符串位移递归函数
%% Str:字符串
%% Step:位移量
%% 返回:位移后的字符串
shift(S, 0) ->
S;
shift([T|S], Step) ->
%或者是:shift(lists:concat([S, [T]]), Step-1).
shift(S ++ [T], Step-1).

%% 测试(bcdef 左旋2位 变成 cdefab)
test() ->
dbg:tracer(),
dbg:p(all,[c]),
dbg:tpl(?MODULE, [{'_', [], [{return_trace}]}]),
start("abcdef", 2).

0 comments on commit bebde86

Please sign in to comment.