标签: traversal

Python - 编写一个函数,将字符串作为参数并向后显示字母,每行一个

这是“如何像计算机科学家一样思考”的练习。我正在学习 Python/编程,但不确定如何完成此任务。

这是书中的一个例子,向前显示字母,我不知道如何获得相反的效果。必须使用while循环。

fruit = 'banana'
index = 0
while index > len(fruit):
        letter = fruit[index]
        print letter
        index = index + 1
Run Code Online (Sandbox Code Playgroud)

python traversal while-loop

1
推荐指数
1
解决办法
2万
查看次数

如何从存储在数组中的嵌套集中获取完整路径?

说我有以下数组:

Array
(
[0] => Array
    (
        [category_id] => 1
        [name] => foo
        [parent_id] => 0
    )

[1] => Array
    (
        [category_id] => 2
        [name] => bar
        [parent_id] => 1
    )

[2] => Array
    (
        [category_id] => 3
        [name] => baz
        [parent_id] => 0
    )

[3] => Array
    (
        [category_id] => 4
        [name] => test
        [parent_id] => 2
    )
[4] => Array
    (
        [category_id] => 5
        [name] => test2
        [parent_id] => 4
    )
Run Code Online (Sandbox Code Playgroud)

)

我正在尝试获得以下输出:

foo
foo > bar
baz …
Run Code Online (Sandbox Code Playgroud)

php arrays nested traversal parent-child

1
推荐指数
1
解决办法
908
查看次数

解析复杂的json jquery

我正在尝试使用以下json数据获取JSON:

{
  "count": 96,
  "value": {
    "title": "cotripWeatherStations",
    "description": "Pipes Output",
    "link": "http:\/\/pipes.yahoo.com\/pipes\/pipe.info?_id=f43ccc14ab9d1c88d537236e93c1525e",
    "pubDate": "Mon, 03 Sep 2012 02:17:48 +0000",
    "generator": "http:\/\/pipes.yahoo.com\/pipes\/",
    "callback": "",
    "items": [
      {
        "ws:Location": {
          "global:Latitude": "37.086807",
          "global:Longitude": "-104.521294"
        },
        "ws:Device": {
          "global:DeviceId": "6796",
          "global:CommonName": "025N008  RATON PASS (RWIS)",
          "global:Status": "enabled",
          "global:EntityId": "1",
          "global:RoadId": "31",
          "global:RoadName": "I-25",
          "global:Direction": "North",
          "global:MileMarker": "8",
          "global:LastUpdateDate": "2012-08-26T13:52:00.000-06:00",
          "global:Icon": "theme\/cotrip.org\/images\/devices\/icon_device_weather_station_with_cam_16x23.gif"
        }
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

以下代码为我提供了输出警报,但没有数据:

var weather = $.getJSON("http://pipes.yahoo.com/pipes/pipe.run?_id=f43ccc14ab9d1c88d537236e93c1525e&_render=json&_callback=?", function(json) {
  alert("JSON Data: " + json.value["items"]);
});
Run Code Online (Sandbox Code Playgroud)

我如何进一步遍历这个JSON到'ws:Devise - "global:CommonName"

javascript arrays jquery json traversal

1
推荐指数
1
解决办法
3582
查看次数

在clojure中转换嵌套映射

我有一个嵌套的地图,看起来像这样:

{"a" {:points 2} "b" {:points 7} "c" {:points 1} "d" {:points 3}}
Run Code Online (Sandbox Code Playgroud)

我想把它变成一个地图序列,以便排序(排序)并在之后打印.

({:name "a" :points 2}
 {:name "b" :points 7}
 {:name "c" :points 1}
 {:name "d" :points 3})
Run Code Online (Sandbox Code Playgroud)

从文档中我发现我需要像邮路一样的东西,但我无法绕过它.

traversal clojure map

1
推荐指数
1
解决办法
299
查看次数

Python Pyramid - 带有shash的URL调度和遍历问题(/)

我有一个关于URL Dispatch或Traversal的初学者问题.我想要做的是使用Mako渲染器渲染我的观点.所以我有这个文件夹/ includes /所有的html和css和javascript.这是我的金字塔主要代码:

from pyramid.config import Configurator

def main(global_config, **settings):
    config = Configurator(settings=settings)
    config.add_static_view('includes', 'includes', cache_max_age=3600)
    config.add_renderer(".html", "pyramid.mako_templating.renderer_factory")  
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()
Run Code Online (Sandbox Code Playgroud)

这些是我的看法:

@view_config(route_name='home', renderer='index.html')
def my_view(request):
    return {'name':'Netherdrake'}

@view_config(name = 'login', renderer='login.html')
def login(request):
    return {'name':'Netherdrake'}
Run Code Online (Sandbox Code Playgroud)

问题是,当我访问ip:port/login网站工作正常,但当我尝试ip:port/login/css,javascript和图像不起作用.原因是,我的绝对路径无效.

这是我的login.html片段(真的是mako模板)而没有/登录时:

 <link href="includes/css/twitter/bootstrap.css" rel="stylesheet">
  <link href="includes/css/base.css" rel="stylesheet">
  <link href="includes/css/twitter/responsive.css" rel="stylesheet">
  <link href="includes/css/jquery-ui-1.8.23.custom.css" rel="stylesheet">
  <script src="includes/js/plugins/modernizr.custom.32549.js"></script>
Run Code Online (Sandbox Code Playgroud)

在这里,我正在登录/路径(在这种情况下网站被破坏,没有css,图像,js ......):

  <link href="includes/css/twitter/bootstrap.css" rel="stylesheet">
  <link href="includes/css/base.css" rel="stylesheet">
  <link href="includes/css/twitter/responsive.css" rel="stylesheet">
  <link href="includes/css/jquery-ui-1.8.23.custom.css" rel="stylesheet">
  <script src="includes/js/plugins/modernizr.custom.32549.js"></script>
Run Code Online (Sandbox Code Playgroud)

login.html也在/包含.

我如何解决这个问题,并使其在两条路径下工作,有无斜线?我尝试了几个和url调度,两个问题都是一样的.

python url traversal pyramid

1
推荐指数
1
解决办法
290
查看次数

是否可以从下往上遍历文件?[C]

如何从下往上逐行遍历文件?例如,这是我从上到下遍历它的代码:

void *readFileTB(char *str1)
{
    int size = 1024;
    char *buffer = malloc(size);
    FILE *fp;
    fp = fopen("testfile.txt", "r");
    while(fgets(buffer, 1024, fp)) //get a line from a file
    {
            printf(buffer);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果文件包含:

line1onetest
line2twotest
line3threetest
Run Code Online (Sandbox Code Playgroud)

如果执行此功能将打印以下内容:

line1onetest
line2twotest
line3threetest
Run Code Online (Sandbox Code Playgroud)

我怎样才能编写一个能够完成上述操作但功能相反的函数,以便输出以下内容?

line3threetest
line2twotest
line1onetest
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

c traversal file

1
推荐指数
1
解决办法
544
查看次数

通过引用array_walk()传递数组后的空数组

我做了这个小班:

class Analyzer {
    public static function analyze($phrases) {
        $sortedPhrases = array();
        array_walk($phrases, array('self', 'splitByLength'), $sortedPhrases);
        var_dump($sortedPhrases);
    }

    private static function splitByLength($item, $key, &$resArr) {
        // line stolen from here: http://stackoverflow.com/a/4786840/603003
        // thanks to arnaud576875 <http://stackoverflow.com/users/576875/arnaud576875>
        $len = count( preg_split('#\PL+#u', $item, -1, PREG_SPLIT_NO_EMPTY) );
        if (!isset($resArr[$len])) {
            $resArr[$len] = array();
        }
        $resArr[$len][] = $item;

        var_dump($resArr);
    }
}

$phrases = array(
    "I can't believe the great content",
    "I can't understand the superior information",
    "I can't comprehend the amazing data",
    "I cannot …
Run Code Online (Sandbox Code Playgroud)

php arrays traversal reference

1
推荐指数
1
解决办法
916
查看次数

霍夫曼树:穿越

我不确定我将如何攻击我的霍夫曼树的穿越.树是正确的,我只是很难确定如何以一种好的方式遍历它.出于某种原因,我的遍历方法没有结果......

更新:清理代码,使其更加面向对象

节点类:

public class Node
{
    public int frekvens; //Frequency
    public char tegn; //Symbol
    public Node venstre; //Left child
    public Node høyre; //Right child
    public string s; //result string
    public string resultat;
    public Node (char c) // Node constructor containing symbol.
    {
        frekvens = 1;
        tegn = c;
    }

    public Node (int f, Node venstre, Node høyre) // Node Constructor containing frequency and children
        {
            frekvens = f;
            this.venstre = venstre;
            this.høyre = høyre;
        }

    public Node (Node node) …
Run Code Online (Sandbox Code Playgroud)

c# recursion traversal huffman-code

1
推荐指数
1
解决办法
4846
查看次数

获取条目的所有后代

我有一个庞大的推荐系统(超过 500k 个条目),其工作原理如下

 [{id: 1, name: "John", ref: 0},
  {id: 2, name: "Jack", ref: 1},
  {id: 3, name: "Bill", ref: 1},
  {id: 5, name: "Jason", ref: 2},
  {id: 6, name: "James", ref: 3},
  {id: 7, name: "Tom", ref: 0}]
Run Code Online (Sandbox Code Playgroud)

每当用户使用其他用户的推荐代码加入时,推荐人都会获得一些积分,并且它适用于所有级别,因此在此示例中,John该 ID 会获得积分[2, 3, 5, 6]

我使用此方法根据推荐 ID 来统计和组织所有条目

   const countRefs = (list) => {
      return list.reduce((acc, cur) => {
        if(!acc[cur.ref]) acc[cur.ref] = [];
        acc[cur.ref].push(cur.id);
        return acc;
      },{});
    }
Run Code Online (Sandbox Code Playgroud)

然后使用此递归函数通过 ID 获取所有引用的用户。

let depth = 0;
// Keep …
Run Code Online (Sandbox Code Playgroud)

javascript recursion loops traversal node.js

1
推荐指数
1
解决办法
280
查看次数

什么是就地算法?

假设我想从字符串中删除重复项.我决定使用长度为256的布尔数组来存储特定字符是否已经出现.我可以遍历字符串,并可以借助此辅助布尔数组删除所有重复项.

我的问题是"这个算法是否就位?"

我认为它使用的是恒定的空间量,它不会随着它应该就地输入的大小而改变.如果我错了,请纠正.

string algorithm traversal duplicates in-place

0
推荐指数
1
解决办法
167
查看次数